I am trying to select an entire row in a different sheet and then copy the row to the sheet I am currently in with a macro. The code works fine if the Rows() sub is passed integers hardcoded but when I put a variable I get the "Select method of Range class failed" error. here is the code I have:
Sheets("BOM").Select
Rows(copyFromRow & ":" & copyFromRow).Select
Selection.Copy
Sheets("Proposal").Select
Rows(copyToRow & ":" & copyToRow).Select
ActiveSheet.Paste
copyToRow = copyToRow + 1
Rows(copyToRow & ":" & copyToRow).Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
if instead i used :
Rows("52:52").Select
Selection.Copy
it works fine, but when the variable is there, the error occurs.
Thanks
I just tested the code at the bottom and it prints 16384 twice (I'm on Excel 2010) and the first row gets selected. Your problem seems to be somewhere else.
Have you tried to get rid of the selects:
Sheets("BOM").Rows(copyFromRow).Copy
With Sheets("Proposal")
.Paste Destination:=.Rows(copyToRow)
copyToRow = copyToRow + 1
Application.CutCopyMode = False
.Rows(copyToRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End With
Test code to get convinced that the problem does not seem to be what you think it is.
Sub test()
Dim r
Dim i As Long
i = 1
r = Rows(i & ":" & i)
Debug.Print UBound(r, 2)
r = Rows(i)
Debug.Print UBound(r, 2)
Rows(i).Select
End Sub
I solved the problem for me by addressing also the worksheet first:
ws.rows(x & ":" & y).Select
without the reference to the worksheet (ws) I got an error.
Saw this answer on another site and it works for me as well!
Posted by Shawn on October 14, 2001 1:24 PM
var1 = 1
var2 = 5
Rows(var1 & ":" & var2).Select
That worked for me, looks like you just have to keep the variables outside the quotes and add the and statement (&)
-Shawn
The key is in the quotes around the colon and &, i.e. rows(variable & ":" & variable).select
Adapt this:
Rows(x & ":" & y).select
where x and y are your variables.
Some other examples that may help you understand
Rows(x & ":" & x).select
Or
Rows((x+1) & ":" (x*3)).select
Or
Rows((x+2) & ":" & (y-3)).select
Hopefully you get the idea.
You need to add quotes. VBA is translating
Rows(copyToRow & ":" & copyToRow).Select`
into
Rows(52:52).Select
Try changing
Rows(""" & copyToRow & ":" & copyToRow & """).Select
One needs to make sure the space between the variables and '&' sign.
Check the image. (Red one showing invalid commands)
The correct solution is
Dim copyToRow: copyToRow = 5
Rows(copyToRow & ":" & copyToRow).Select
mycell = ActiveCell
r = mycell.Row
c = mycell.Column
Range(Cells(r, c), Cells(r + 100, c)).Select
Related
Looking for some help on a small project of mine. VBA coding is not my thing and just started to learn now at a way later age than I'd like.
Backstory:
I work in Business Management and look at a lot of project management finances. I see that it is pretty slow to add a new formula and then drag down the updated formula.
I have been looking how to add a new row that is continuous on top of adding a Concatenate formula to provide a unique ID.
I have done some research and found a file that will be able to add rows with a value of +1 for every row.
Goal(s):
Main Objective-
If someone can help me to update the formula (Concatenate formula on column E), it is a straight copy and paste at this moment so it is not updating based on the new row. Can someone help me revise the code?
Secondary Objective-
Also, it may be a reach but is there code out already that can ask for a text box of some sort to provide more than one new row with the appropriate formulas being copied as well?
Thank you for the help!
Please see the code below:
Sub Button1_Click()
Dim wsS1 As Worksheet
Dim lRows As Long
Dim Iint As Integer
Set wsS1 = Worksheets("Sheet1")
lRows = wsS1.Range("B65536").End(xlUp).Row
If wsS1.Range("B5").Value = "" Then
wsS1.Range("B5").Value = 1
wsS1.Range("G5").Formula = "=IF(AND(D4=""""),"""",IF(AND(D4<>""""),$E4))"
wsS1.Rows(4).EntireRow.Copy
wsS1.Rows(5).PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.Interior.ColorIndex = xlNone
Selection.Font.Bold = False
Rows(5).RowHeight = 12.75
End If
For Iint = 5 To lRows
If wsS1.Range("B" & Iint + 1).Value = "" Then
wsS1.Rows(Iint).EntireRow.Copy
wsS1.Rows(Iint + 1).PasteSpecial xlFormats
wsS1.Rows(Iint + 1).PasteSpecial xlFormula
wsS1.Range("G" & Iint + 1).Formula = wsS1.Range("G" & Iint).Formula
wsS1.Range("C" & Iint + 1).Formula = wsS1.Range("C" & Iint).Formula
wsS1.Range("D" & Iint + 1).Formula = wsS1.Range("D" & Iint).Formula
wsS1.Range("E" & Iint + 1).Formula = wsS1.Range("E" & Iint).Formula
wsS1.Range("B" & Iint + 1).Value = wsS1.Range("B" & Iint).Value + 1
End If
Next Iint
End Sub
I want to put an if-formula into a cell.
I get an error 1004 when I try to run the code (relevant part below).
n = 1.2
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=1," & n & ",na())"
If I put the formula into a variable, I can see that VBA "saves" the 1.2 as "1,2" which is interpreted as if there are too many arguments in the formula.
So instead of
=if(C2=1,1.2,na())
I get
=if(C2=1,1,2,na())
If I manually enter
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=0,1.2" & ",na())"
The formula comes out right in Excel, but as I need to loop over a data, that is not a sustainable solution.
As I am based in Europe, I suspect regional settings being a part of the problem, but as those are company-wide, I can't fiddle with that without causing a different set of problems.
I tried
n = 1.2
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=1," & CDbl(n) & ",na())"
I searched on internet, without any help coming out of that...
I simply would like to be able to have my VBA code to write this formula in a cell :
=IF(C4="-"; "-"; Cars!C4*C4*Data!$C$8)
As you guessed, there is a page called "Cars" and one called "Data" where I pick the informations needed.
Of course, as it is a VBA code, the C4 will be 2 variables, one for the C and one for the 4 that will evolve...
Actually, I tried this :
Worksheets("Calculation").Range(Column & PosStartCalc + 1).Formula = "=" & "IF(" & Column & PosStartCalc & " = " & "" - "" & ";" & " - " & ";" & "Cars!" & Column & PosStart & "*" & Column & PosStartCalc & "*" & "Data!" & "C" & "8" & ")"
(The variable Column contains the column letter and the variable PosStartCalc contains the row number)
This hurts my eyes and apparently VBA's ones too as it gives the error "Run-Time error '13': Type Mismatch'
Could anyone tell me how to do that?
Thanks in advance !
Try the following, assuming the column variable is a string and row a long variable. I might not have all the variables right, but you'll be able to get what I meant to do here.
Sub test()
Dim Col As String: Col = "C"
Dim Rw As Long: Rw = 4
With ThisWorkbook.Sheets("Calculation")
Debug.Print "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
.Cells(Rw + 1, Col).Formula = "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
End With
End Sub
So what you might forget easily is to use the , as parameter delimiter in a VBA programmed formula. When you put this on your sheet Excel will automatically replace that with the appropriate delimiter for your region.
Another thing to keep in mind; whenever you about to use a string value in such an function, don't forget to wrap it in double quotes!
Don't forget to remove the Debug.print .... line. It was merely there to show the output :)
The below code is copying the data fine, however the copy when then number is copied over is copied as text. Is there any way to copy it over as a number?
Worksheets(1).Range(destCol1 & LR & destCol2 & LR).cells(3).value = Split(srcRange(5).value)
I've tried recording a macro where I'd highlight the data and click the option "Convert text to number" but it's not recording the VBA for that part.
EDIT 1:
With Worksheets(1).Range(destCol1 & LR & destCol2 & LR)
.cells(1).value = srcRange(2).text
.cells(2).value = srcRange(1).value
.cells(3).value = CDbl(Split(srcRange(5).value)(1))
.cells(4).value = srcRange(6).value
End With
Untested:
Sub jdskfhs()
Worksheets(1).Range(destCol1 & LR & destCol2 & LR).Cells(3).Value = CDbl(Split(srcRange(5).Value))
End Sub
EDIT#1:
Here is what you need:
Sub jdskfhs2()
Worksheets(1).Range(destCol1 & LR & destCol2 & LR).Cells(3).Value = CDbl(Split(srcRange(5).Value)(1))
End Sub
This new code converts the second element of the Split()-generated array to a number.
After quite a bit of digging, I found another answer on these forums here
I would ideally like to have it all done it one line to reduce processing power as these pages can get quite large; unfortunately this was the best solution I could get.
EDIT:
Using the below code that I modified a bit, but it does work
Private Function TextToNumber(rng As Range)
LR = ActiveSheet.Range("C" & Rows.Count).End(xlUp).row
For R = LR To 3 Step -1
With rng.cells(R, 1)
.NumberFormat = "General"
.value = .value
End With
Next R
End Function
I'm trying to get this segment of code to execute. This is a simplified version of the code. I've included the relevant code. I'm trying to concatenate strings and named ranges into a SumIfs formula, but I get error 1004 "Application-defined or Object-defined error." I have a working line of code above this problem section that is similar with the exception of doing a sum function, instead of sumif. Any idea how to get this code to execute? Thank you.
Dim wb As Workbook
Dim ara As Worksheet
Dim inv As Worksheet
Dim ARBlock As Range
Dim Invoices As Range
Dim AgedDays As Range
Set wb = ThisWorkbook
Set ara = wb.Sheets("AR Aging")
Set inv = wb.Sheets("Invoices")
Set ARBlock = ara.Range("a6")
Set Invoices = inv.Range("a6", inv.Range("a6").End(xlDown))
Set AgedDays = Invoices.Offset(0, 6)
'Populate A/R age buckets
For i = 6 To ARBlock.Rows.Count + 6
With ARBlock(i - 5, 1).Offset(0, 3)
.Value = "=SumIfs(" & Invoices.Offset(0, 4).Address & "," & _
Invoices.Address & "," & ARBlock(i - 5, 1).Value & "," & _
Invoices.Offset(0, 6).Address & ","" <= "" & &O30)"
End With
Next i
End Sub
The line beginning with ".value" is where I'm getting the error message. P.S.: I need the cell to contain the concatenated formula, as opposed to the output value.
UPDATE 1:
As some suggested I updated the .value line to:
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
I'm still getting the same error. Some auditing I've done:
Removing the "=" before "Sumifs" allows the code to run fine; pasting in the formula into the target cell as text. In this form, my output for i=1 goes to ARBlock.cells(1,1), as it should.
I also used Debug.Print to view all of the components of the formula:
Debug.Print ARBlock.Cells(i - 5, 1).Address
'output $A$6
Debug.Print ARBlock.Cells(i - 5, 1).Value
' output International Business Machines
Debug.Print Invoices.Offset(0, 4).Address
'output $E$6:$E$255
Debug.Print Invoices.Address
'output $A$6:$A$255
I suspected the issue might be that the range dimensions might have been off, but this is not the case. My next suspicion is that the output International Business Machines needs to be in " " for the formula to read it correctly. I hardcoded in
""International Business Machines""
to see if this would fix the formula, but I keep getting the same error once I add the "=" back in. The formula syntax is correct, the dimensions are the same between the sum range and criteria range. Anyone else have any ideas?
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
Change to:
.Offset(0, 3).Formula = "=SumIfs(" & Invoices.Offset(0, 4).Address & ", " & Invoices.Address & ", " & chr(34) & ARBlock.Cells(i - 5, 1).Value & chr(34) & ")"
EDIT: Added quotes chr(34) around your string!
Your ARBlock(i - 5, 1).Value most likely is an empty cell, which messes the SUMIFS formula as it builds it with to consecutive commas