Passing text from one worksheet to another using an array












3















I'm trying to pass data from sheet 3 to sheet 4 based on a criterion (*). With numbers results but with text the program fails.
How to overcome this situation when instead of a number I have text.



Public Sub TestArray3()
'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*"
Dim tempVar As Integer, anotherIteration As Boolean, i As Integer
Dim J As Integer, ArraySize As Integer, myArray() As Integer
Dim newArray() As Integer, FinalRow As Integer, linha As Integer
Dim counter As Integer, cel1 As Range

Sheets("Folha3").Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(Cells(linha, "B").Value) 'Populate de Array
End If
Next linha

ReDim newArray(LBound(myArray) To UBound(myArray)) 'Avoid zeros in Array
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "0" Then
J = J + 1
newArray(J) = myArray(i)
End If
Next i

ReDim Preserve newArray(LBound(myArray) To J)
ArraySize = J
Sheets("Folha4").Select 'Write data to Sheet 4 column A
Range("A1").Resize(J - LBound(newArray) + 1)=Application.Transpose(newArray)
End Sub









share|improve this question




















  • 1





    Why do you use an unqualified Val(...) conversion if you are expecting text?

    – user4039065
    Oct 1 '18 at 22:15











  • Do you mean to use Val so that text are zeroes?

    – user4039065
    Oct 1 '18 at 22:21











  • Which range are you trying to copy, and to where?

    – ashleedawg
    Oct 1 '18 at 23:17
















3















I'm trying to pass data from sheet 3 to sheet 4 based on a criterion (*). With numbers results but with text the program fails.
How to overcome this situation when instead of a number I have text.



Public Sub TestArray3()
'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*"
Dim tempVar As Integer, anotherIteration As Boolean, i As Integer
Dim J As Integer, ArraySize As Integer, myArray() As Integer
Dim newArray() As Integer, FinalRow As Integer, linha As Integer
Dim counter As Integer, cel1 As Range

Sheets("Folha3").Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(Cells(linha, "B").Value) 'Populate de Array
End If
Next linha

ReDim newArray(LBound(myArray) To UBound(myArray)) 'Avoid zeros in Array
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "0" Then
J = J + 1
newArray(J) = myArray(i)
End If
Next i

ReDim Preserve newArray(LBound(myArray) To J)
ArraySize = J
Sheets("Folha4").Select 'Write data to Sheet 4 column A
Range("A1").Resize(J - LBound(newArray) + 1)=Application.Transpose(newArray)
End Sub









share|improve this question




















  • 1





    Why do you use an unqualified Val(...) conversion if you are expecting text?

    – user4039065
    Oct 1 '18 at 22:15











  • Do you mean to use Val so that text are zeroes?

    – user4039065
    Oct 1 '18 at 22:21











  • Which range are you trying to copy, and to where?

    – ashleedawg
    Oct 1 '18 at 23:17














3












3








3








I'm trying to pass data from sheet 3 to sheet 4 based on a criterion (*). With numbers results but with text the program fails.
How to overcome this situation when instead of a number I have text.



Public Sub TestArray3()
'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*"
Dim tempVar As Integer, anotherIteration As Boolean, i As Integer
Dim J As Integer, ArraySize As Integer, myArray() As Integer
Dim newArray() As Integer, FinalRow As Integer, linha As Integer
Dim counter As Integer, cel1 As Range

Sheets("Folha3").Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(Cells(linha, "B").Value) 'Populate de Array
End If
Next linha

ReDim newArray(LBound(myArray) To UBound(myArray)) 'Avoid zeros in Array
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "0" Then
J = J + 1
newArray(J) = myArray(i)
End If
Next i

ReDim Preserve newArray(LBound(myArray) To J)
ArraySize = J
Sheets("Folha4").Select 'Write data to Sheet 4 column A
Range("A1").Resize(J - LBound(newArray) + 1)=Application.Transpose(newArray)
End Sub









share|improve this question
















I'm trying to pass data from sheet 3 to sheet 4 based on a criterion (*). With numbers results but with text the program fails.
How to overcome this situation when instead of a number I have text.



Public Sub TestArray3()
'Array to copy data from Sheet3 to Sheet4 Based on criterion "in this case*"
Dim tempVar As Integer, anotherIteration As Boolean, i As Integer
Dim J As Integer, ArraySize As Integer, myArray() As Integer
Dim newArray() As Integer, FinalRow As Integer, linha As Integer
Dim counter As Integer, cel1 As Range

Sheets("Folha3").Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(Cells(linha, "B").Value) 'Populate de Array
End If
Next linha

ReDim newArray(LBound(myArray) To UBound(myArray)) 'Avoid zeros in Array
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "0" Then
J = J + 1
newArray(J) = myArray(i)
End If
Next i

ReDim Preserve newArray(LBound(myArray) To J)
ArraySize = J
Sheets("Folha4").Select 'Write data to Sheet 4 column A
Range("A1").Resize(J - LBound(newArray) + 1)=Application.Transpose(newArray)
End Sub






arrays excel vba excel-vba transpose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 1 '18 at 23:14









ashleedawg

12.7k42351




12.7k42351










asked Oct 1 '18 at 22:13









Fernando QuaresmaFernando Quaresma

162




162








  • 1





    Why do you use an unqualified Val(...) conversion if you are expecting text?

    – user4039065
    Oct 1 '18 at 22:15











  • Do you mean to use Val so that text are zeroes?

    – user4039065
    Oct 1 '18 at 22:21











  • Which range are you trying to copy, and to where?

    – ashleedawg
    Oct 1 '18 at 23:17














  • 1





    Why do you use an unqualified Val(...) conversion if you are expecting text?

    – user4039065
    Oct 1 '18 at 22:15











  • Do you mean to use Val so that text are zeroes?

    – user4039065
    Oct 1 '18 at 22:21











  • Which range are you trying to copy, and to where?

    – ashleedawg
    Oct 1 '18 at 23:17








1




1





Why do you use an unqualified Val(...) conversion if you are expecting text?

– user4039065
Oct 1 '18 at 22:15





Why do you use an unqualified Val(...) conversion if you are expecting text?

– user4039065
Oct 1 '18 at 22:15













Do you mean to use Val so that text are zeroes?

– user4039065
Oct 1 '18 at 22:21





Do you mean to use Val so that text are zeroes?

– user4039065
Oct 1 '18 at 22:21













Which range are you trying to copy, and to where?

– ashleedawg
Oct 1 '18 at 23:17





Which range are you trying to copy, and to where?

– ashleedawg
Oct 1 '18 at 23:17












2 Answers
2






active

oldest

votes


















1














I'm not clear on where you're actually trying to paste from/to, but here's one [of several] ways to move data between worksheets, including both with and without transposing



Hopefully this example should clear up the steps:



Sub copyRangeToOtherSheet()

Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

With ThisWorkbook.Sheets("Sheet1") 'set source worksheet

lastRow = .Cells(Rows.Count, "A").End(xlUp).Row 'find last row of Col A
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'find last col of Row 1
Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol)) 'create range (from A1)

End With

arr = rgSrc 'dump range into array

With ThisWorkbook.Sheets("Sheet2") 'set destination sheet

'OPTION #1: Populate destination in "original" orientation
Set rgDest = .Range("A1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2)) 'fit to array rows/col's
rgDest = arr 'dump array to worksheet range

'OPTION #2: Populate destination in "transposed" orientation
Set rgDest = .Range("D1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1)) 'fit to array col's/rows
rgDest = WorksheetFunction.Transpose(arr) 'dump transposed array to worksheet range

End With

End Sub


Note that it's easiest if you don't set the size of the array in advance — Excel will size it automatically as long as the array isn't already dimensioned (which is why it's declared only as arr() As Variant).



On the destination end, we can pick one cell as the top-left of the range, then ReSize the range based on the arrays' upper bounds (UBound).



If we are going to Transpose the cells, we must swap the number of rows/columns in the destination range.



img





More Information:




  • One resource I've found very helpful is Chip Pearson's VBA Arrays And Worksheet Ranges.






share|improve this answer
























  • Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

    – Fernando Quaresma
    Oct 2 '18 at 21:02



















0














String vs Integer



It is a little unclear what is happening here, but I have noticed that you have declared all your arrays as integer so you cannot pass strings to them. Try to find out which array you're trying to pass strings to and declare it as variant or implement some 'conditional' code like:



If Not IsNumeric(Cells("A1").Value) then
Variable = 0
End If


Read ashleedawg's guidelines.



You don't have to select a worksheet to do stuff to it (referring to Select). You can write



FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row


or



Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)


and save a line but more importantly, not jump around in the workboook. Even better is using With:



With Sheets("Folha3")
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = .Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
End If
Next linha
End With


Notice the '.' in front of each cells (.cells), it is referring to the sheet object.



Try using variables for objects. When you write



Sheets("folha3"). 


nothing happens you have to remember what it can do. But if you assign it to a variable the intelliSense is activated and you can see the properties and methods of objects e.g.



Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")


Now when you write:



oWs.


the IntelliSense shows you the properties and methods of the worksheet object e.g. Activate, Cells, Copy, Delete, Paste etc.



With a few more lines of code you will learn much more.






share|improve this answer


























  • Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

    – Fernando Quaresma
    Oct 2 '18 at 21:07











  • My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

    – VBasic2008
    Oct 6 '18 at 23:59













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52599586%2fpassing-text-from-one-worksheet-to-another-using-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I'm not clear on where you're actually trying to paste from/to, but here's one [of several] ways to move data between worksheets, including both with and without transposing



Hopefully this example should clear up the steps:



Sub copyRangeToOtherSheet()

Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

With ThisWorkbook.Sheets("Sheet1") 'set source worksheet

lastRow = .Cells(Rows.Count, "A").End(xlUp).Row 'find last row of Col A
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'find last col of Row 1
Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol)) 'create range (from A1)

End With

arr = rgSrc 'dump range into array

With ThisWorkbook.Sheets("Sheet2") 'set destination sheet

'OPTION #1: Populate destination in "original" orientation
Set rgDest = .Range("A1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2)) 'fit to array rows/col's
rgDest = arr 'dump array to worksheet range

'OPTION #2: Populate destination in "transposed" orientation
Set rgDest = .Range("D1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1)) 'fit to array col's/rows
rgDest = WorksheetFunction.Transpose(arr) 'dump transposed array to worksheet range

End With

End Sub


Note that it's easiest if you don't set the size of the array in advance — Excel will size it automatically as long as the array isn't already dimensioned (which is why it's declared only as arr() As Variant).



On the destination end, we can pick one cell as the top-left of the range, then ReSize the range based on the arrays' upper bounds (UBound).



If we are going to Transpose the cells, we must swap the number of rows/columns in the destination range.



img





More Information:




  • One resource I've found very helpful is Chip Pearson's VBA Arrays And Worksheet Ranges.






share|improve this answer
























  • Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

    – Fernando Quaresma
    Oct 2 '18 at 21:02
















1














I'm not clear on where you're actually trying to paste from/to, but here's one [of several] ways to move data between worksheets, including both with and without transposing



Hopefully this example should clear up the steps:



Sub copyRangeToOtherSheet()

Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

With ThisWorkbook.Sheets("Sheet1") 'set source worksheet

lastRow = .Cells(Rows.Count, "A").End(xlUp).Row 'find last row of Col A
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'find last col of Row 1
Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol)) 'create range (from A1)

End With

arr = rgSrc 'dump range into array

With ThisWorkbook.Sheets("Sheet2") 'set destination sheet

'OPTION #1: Populate destination in "original" orientation
Set rgDest = .Range("A1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2)) 'fit to array rows/col's
rgDest = arr 'dump array to worksheet range

'OPTION #2: Populate destination in "transposed" orientation
Set rgDest = .Range("D1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1)) 'fit to array col's/rows
rgDest = WorksheetFunction.Transpose(arr) 'dump transposed array to worksheet range

End With

End Sub


Note that it's easiest if you don't set the size of the array in advance — Excel will size it automatically as long as the array isn't already dimensioned (which is why it's declared only as arr() As Variant).



On the destination end, we can pick one cell as the top-left of the range, then ReSize the range based on the arrays' upper bounds (UBound).



If we are going to Transpose the cells, we must swap the number of rows/columns in the destination range.



img





More Information:




  • One resource I've found very helpful is Chip Pearson's VBA Arrays And Worksheet Ranges.






share|improve this answer
























  • Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

    – Fernando Quaresma
    Oct 2 '18 at 21:02














1












1








1







I'm not clear on where you're actually trying to paste from/to, but here's one [of several] ways to move data between worksheets, including both with and without transposing



Hopefully this example should clear up the steps:



Sub copyRangeToOtherSheet()

Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

With ThisWorkbook.Sheets("Sheet1") 'set source worksheet

lastRow = .Cells(Rows.Count, "A").End(xlUp).Row 'find last row of Col A
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'find last col of Row 1
Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol)) 'create range (from A1)

End With

arr = rgSrc 'dump range into array

With ThisWorkbook.Sheets("Sheet2") 'set destination sheet

'OPTION #1: Populate destination in "original" orientation
Set rgDest = .Range("A1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2)) 'fit to array rows/col's
rgDest = arr 'dump array to worksheet range

'OPTION #2: Populate destination in "transposed" orientation
Set rgDest = .Range("D1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1)) 'fit to array col's/rows
rgDest = WorksheetFunction.Transpose(arr) 'dump transposed array to worksheet range

End With

End Sub


Note that it's easiest if you don't set the size of the array in advance — Excel will size it automatically as long as the array isn't already dimensioned (which is why it's declared only as arr() As Variant).



On the destination end, we can pick one cell as the top-left of the range, then ReSize the range based on the arrays' upper bounds (UBound).



If we are going to Transpose the cells, we must swap the number of rows/columns in the destination range.



img





More Information:




  • One resource I've found very helpful is Chip Pearson's VBA Arrays And Worksheet Ranges.






share|improve this answer













I'm not clear on where you're actually trying to paste from/to, but here's one [of several] ways to move data between worksheets, including both with and without transposing



Hopefully this example should clear up the steps:



Sub copyRangeToOtherSheet()

Dim lastRow As Long, lastCol As Long, rgSrc As Range, rgDest As Range, arr() As Variant

With ThisWorkbook.Sheets("Sheet1") 'set source worksheet

lastRow = .Cells(Rows.Count, "A").End(xlUp).Row 'find last row of Col A
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'find last col of Row 1
Set rgSrc = Range(.Range("A1"), .Cells(lastRow, lastCol)) 'create range (from A1)

End With

arr = rgSrc 'dump range into array

With ThisWorkbook.Sheets("Sheet2") 'set destination sheet

'OPTION #1: Populate destination in "original" orientation
Set rgDest = .Range("A1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 1), UBound(arr, 2)) 'fit to array rows/col's
rgDest = arr 'dump array to worksheet range

'OPTION #2: Populate destination in "transposed" orientation
Set rgDest = .Range("D1") 'set destination top-left corner
Set rgDest = rgDest.Resize(UBound(arr, 2), UBound(arr, 1)) 'fit to array col's/rows
rgDest = WorksheetFunction.Transpose(arr) 'dump transposed array to worksheet range

End With

End Sub


Note that it's easiest if you don't set the size of the array in advance — Excel will size it automatically as long as the array isn't already dimensioned (which is why it's declared only as arr() As Variant).



On the destination end, we can pick one cell as the top-left of the range, then ReSize the range based on the arrays' upper bounds (UBound).



If we are going to Transpose the cells, we must swap the number of rows/columns in the destination range.



img





More Information:




  • One resource I've found very helpful is Chip Pearson's VBA Arrays And Worksheet Ranges.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 2 '18 at 0:29









ashleedawgashleedawg

12.7k42351




12.7k42351













  • Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

    – Fernando Quaresma
    Oct 2 '18 at 21:02



















  • Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

    – Fernando Quaresma
    Oct 2 '18 at 21:02

















Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

– Fernando Quaresma
Oct 2 '18 at 21:02





Thank you for your help but, I have in sheet 1 in column A I have * and in column B I have string. I want to copy every time I find the * the macro copy from sheet1 the string to sheet 2. When I execute the macro the system see the string with 0.

– Fernando Quaresma
Oct 2 '18 at 21:02













0














String vs Integer



It is a little unclear what is happening here, but I have noticed that you have declared all your arrays as integer so you cannot pass strings to them. Try to find out which array you're trying to pass strings to and declare it as variant or implement some 'conditional' code like:



If Not IsNumeric(Cells("A1").Value) then
Variable = 0
End If


Read ashleedawg's guidelines.



You don't have to select a worksheet to do stuff to it (referring to Select). You can write



FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row


or



Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)


and save a line but more importantly, not jump around in the workboook. Even better is using With:



With Sheets("Folha3")
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = .Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
End If
Next linha
End With


Notice the '.' in front of each cells (.cells), it is referring to the sheet object.



Try using variables for objects. When you write



Sheets("folha3"). 


nothing happens you have to remember what it can do. But if you assign it to a variable the intelliSense is activated and you can see the properties and methods of objects e.g.



Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")


Now when you write:



oWs.


the IntelliSense shows you the properties and methods of the worksheet object e.g. Activate, Cells, Copy, Delete, Paste etc.



With a few more lines of code you will learn much more.






share|improve this answer


























  • Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

    – Fernando Quaresma
    Oct 2 '18 at 21:07











  • My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

    – VBasic2008
    Oct 6 '18 at 23:59


















0














String vs Integer



It is a little unclear what is happening here, but I have noticed that you have declared all your arrays as integer so you cannot pass strings to them. Try to find out which array you're trying to pass strings to and declare it as variant or implement some 'conditional' code like:



If Not IsNumeric(Cells("A1").Value) then
Variable = 0
End If


Read ashleedawg's guidelines.



You don't have to select a worksheet to do stuff to it (referring to Select). You can write



FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row


or



Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)


and save a line but more importantly, not jump around in the workboook. Even better is using With:



With Sheets("Folha3")
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = .Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
End If
Next linha
End With


Notice the '.' in front of each cells (.cells), it is referring to the sheet object.



Try using variables for objects. When you write



Sheets("folha3"). 


nothing happens you have to remember what it can do. But if you assign it to a variable the intelliSense is activated and you can see the properties and methods of objects e.g.



Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")


Now when you write:



oWs.


the IntelliSense shows you the properties and methods of the worksheet object e.g. Activate, Cells, Copy, Delete, Paste etc.



With a few more lines of code you will learn much more.






share|improve this answer


























  • Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

    – Fernando Quaresma
    Oct 2 '18 at 21:07











  • My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

    – VBasic2008
    Oct 6 '18 at 23:59
















0












0








0







String vs Integer



It is a little unclear what is happening here, but I have noticed that you have declared all your arrays as integer so you cannot pass strings to them. Try to find out which array you're trying to pass strings to and declare it as variant or implement some 'conditional' code like:



If Not IsNumeric(Cells("A1").Value) then
Variable = 0
End If


Read ashleedawg's guidelines.



You don't have to select a worksheet to do stuff to it (referring to Select). You can write



FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row


or



Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)


and save a line but more importantly, not jump around in the workboook. Even better is using With:



With Sheets("Folha3")
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = .Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
End If
Next linha
End With


Notice the '.' in front of each cells (.cells), it is referring to the sheet object.



Try using variables for objects. When you write



Sheets("folha3"). 


nothing happens you have to remember what it can do. But if you assign it to a variable the intelliSense is activated and you can see the properties and methods of objects e.g.



Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")


Now when you write:



oWs.


the IntelliSense shows you the properties and methods of the worksheet object e.g. Activate, Cells, Copy, Delete, Paste etc.



With a few more lines of code you will learn much more.






share|improve this answer















String vs Integer



It is a little unclear what is happening here, but I have noticed that you have declared all your arrays as integer so you cannot pass strings to them. Try to find out which array you're trying to pass strings to and declare it as variant or implement some 'conditional' code like:



If Not IsNumeric(Cells("A1").Value) then
Variable = 0
End If


Read ashleedawg's guidelines.



You don't have to select a worksheet to do stuff to it (referring to Select). You can write



FinalRow = Sheets("Folha3").Cells(Rows.Count, 1).End(xlUp).Row


or



Sheets("Folha4").Range("A1").Resize(J - LBound(newArray) + 1) _
= Application.Transpose(newArray)


and save a line but more importantly, not jump around in the workboook. Even better is using With:



With Sheets("Folha3")
FinalRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Find the last row of data
ArraySize = FinalRow 'Get Array Size
ReDim myArray(ArraySize - 1)
For linha = 1 To FinalRow
Set cel1 = .Cells(linha, 1)
If cel1 = "*" Then
myArray(linha - 1) = Val(.Cells(linha, "B").Value) 'Populate de Array
End If
Next linha
End With


Notice the '.' in front of each cells (.cells), it is referring to the sheet object.



Try using variables for objects. When you write



Sheets("folha3"). 


nothing happens you have to remember what it can do. But if you assign it to a variable the intelliSense is activated and you can see the properties and methods of objects e.g.



Dim oWb as Workbook
Dim oWs as Worksheet
Set oWb = Activeworkbook
Set oWs = oWb.Sheets("Folha3")


Now when you write:



oWs.


the IntelliSense shows you the properties and methods of the worksheet object e.g. Activate, Cells, Copy, Delete, Paste etc.



With a few more lines of code you will learn much more.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 11:08

























answered Oct 2 '18 at 2:25









VBasic2008VBasic2008

3,1292416




3,1292416













  • Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

    – Fernando Quaresma
    Oct 2 '18 at 21:07











  • My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

    – VBasic2008
    Oct 6 '18 at 23:59





















  • Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

    – Fernando Quaresma
    Oct 2 '18 at 21:07











  • My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

    – VBasic2008
    Oct 6 '18 at 23:59



















Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

– Fernando Quaresma
Oct 2 '18 at 21:07





Thank you for your help and your advise. I used your code (using with) by declaring an array at the beginning but I can't get the macro to read the string, the value is always 0. How to solve?

– Fernando Quaresma
Oct 2 '18 at 21:07













My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

– VBasic2008
Oct 6 '18 at 23:59







My answer's first sentence says you have declared your arrays as integer which is wrong, if you want to pass strings. If you want to pass strings to an array you should declare it as string: Dim SomeArray() as String. Even better is just Dim SomeArray() what is translated to Dim SomeArray() as Variant, then you can pass what ever value you want.

– VBasic2008
Oct 6 '18 at 23:59




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52599586%2fpassing-text-from-one-worksheet-to-another-using-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Futebolista

Feedback on college project

Albești (Vaslui)