简体   繁体   中英

Adding Simple Content to Dynamic Cell Range VBA

I am fairly new to programming and even newer to VBA.

I am attempting to add a column to the beginning of a spreadsheet with "retailer" in "A1" and "RetailerName" in "A2: Last_Relevant_Cell_In_A ".

Here is what I have so far:

Sub AddRetailerName()

Dim WS_Target As Worksheet
Dim WS_Target_Lastrow As Long

Set WS_Target = Worksheets("Sheet1")

'Find last row of WS_Target
WS_Target_Lastrow = WS_Target.Cells.Find("*", [A1], , , _
xlByRows, xlPrevious).Row

'find last column of WS_Target
WS_Target_Lastcol = WS_Target.Cells.Find("*", [A1], , , _
xlByColumns, xlPrevious).Column

Range("A:A").EntireColumn.Insert
Range("A1").FormulaR1C1 = "Retailer"
Range(Cells(2, 1), Cells(WS_Target_Lastcol, 1)).FormulaR1C1 = "RetailerName"


End Sub

This is only inserting content into "A1:A6". The information inserted is correct, but it should be inserting dynamically with how many rows are found in the spreadsheet (in this example, 950).

Any ideas as to how I can fix this?

Note: While this operation is simple to complete with a few clicks (without VBA), I plan to use it on around 20 spreadsheets at once.

You should change

Range(Cells(2, 1), Cells(WS_Target_Lastcol, 1)).FormulaR1C1 = "RetailerName"

to

Range(Cells(2, 1), Cells(WS_Target_Lastrow, 1)).FormulaR1C1 = "RetailerName"

(I assume your last used cell was in column F? That would explain your code populating to row 6.)


It is also a good idea to qualify your Range and Cells (etc) methods with the worksheet that they refer to, so I recommend changing your code to:

Sub AddRetailerName()

    Dim WS_Target As Worksheet
    Dim WS_Target_Lastrow As Long
    Dim WS_Target_Lastcol As Long

    Set WS_Target = Worksheets("Sheet1")

    With WS_Target  ' allows us to use "." instead of "WS_Target."
        'Find last row of WS_Target
        WS_Target_Lastrow = .Cells.Find("*", [A1], , , _
                                        xlByRows, xlPrevious).Row

        'find last column of WS_Target
        WS_Target_Lastcol = .Cells.Find("*", [A1], , , _
                                        xlByColumns, xlPrevious).Column

        .Range("A:A").EntireColumn.Insert
        'Use "Value" rather than "FormulaR1C1" to set a value
        '.Range("A1").FormulaR1C1 = "Retailer"
        '.Range(.Cells(2, 1), .Cells(WS_Target_Lastrow, 1)).FormulaR1C1 = "RetailerName"
        .Range("A1").Value = "Retailer"
        .Range(.Cells(2, 1), .Cells(WS_Target_Lastrow, 1)).Value = "RetailerName"
    End With
End Sub

And just FYI

.Range(.Cells(2, 1), .Cells(WS_Target_Lastrow, 1)).Value = "RetailerName"

could also be written as either

.Range(.Cells(2, "A"), .Cells(WS_Target_Lastrow, "A")).Value = "RetailerName"

or

.Range("A2:A" & WS_Target_Lastrow).Value = "RetailerName"

They each achieve the same thing, but different people prefer different styles of coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM