问题
I am looking to do the following:
When pressing a button, the Macro should be activated. The Macro selects Column H (not the whole column, just until data goes. The last line with data can be determined if after that line the next 10 lines are empty) in Sheet 2 (same workbook). For this selection, the "General" format is applied to every cell. After this, the same runs through for Column G. Then, the Macro ends.
I think it should be easily possible, but I am especially struggling with the "determining last line with data" part, as if applied to the whole column the PC massively slows down.
Then, I am unsure where I should put the code (Sheet, ThisWorkbook, Module) as best practice.
回答1:
There are other ways to do this, but the easiest generally is to choose a LARGE row number (will there ever be more than 20,000 rows?) and navigate upwards.
Range("H2", Range("H20000").End(xlUp)).NumberFormat = "General"
Range("G2", Range("G20000").End(xlUp)).NumberFormat = "General"
But you can also just format the entire columns:
Range("G:H").NumberFormat = "General"
this doesn't (these days) impact the size of the file.
You want to click a button of the sheet to run the macro, so you could use a Form Controls, Button and the code would then be in a standard module.
Added (from my comment):
This would work:
Application.Intersect(ActiveSheet.UsedRange, Range("G:H")).NumberFormat = "General"
but it requires some error-handling, just in case these ranges don't intersect.
Responding to 2 further questions in comments:
Worksheets("Whatever").Activate
If NumberFormat
applied to the column doesn't work then there must be something else going on that interferes - or perhaps the data was faulty when imported(?). Try:
Application.CalculateFull
or just use the specific range:
Range("..").Calculate
If this doesn't work then you may have to copy the data to a blank column and delete the old column. Or perhaps copy and paste (maybe values) into the same range.
回答2:
You can format all cells in columns G and H as general without selecting the range or the sheet. You should always avoid selecting anything in your VBA code.
The de facto standard way of finding the last row with data is to start at bottom of the sheet and go up from there.
The following finds the last cell with data on sheet2 in both columns G and H. We use the greatest of the two to set the range we will use to apply general formatting.
Sub GeneralFormatForAllPopulatedCells()
Dim wb As Workbook
Dim ws As Worksheet
Dim g As Long, h As Long
Dim lastRow As Long
Dim rng As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet2")
g = ws.Range("G" & ws.Rows.Count).End(xlUp).Row
h = ws.Range("H" & ws.Rows.Count).End(xlUp).Row
If g > h Then
lastRow = g
Else
lastRow = h
End If
Set rng = ws.Range("G1:H" & lastRow)
rng.NumberFormat = "General"
End Sub
You should place your code in a module, and make sure that Option Explicit
appears at the top of the module so that variable declaration is required. You can turn this on for all modules by opening the options dialog from within the VBA editor: Tools --> Options and then checking the box next to Require Variable Declaration.
来源:https://stackoverflow.com/questions/17176045/select-column-and-apply-general-format-to-all-cells-put-macro-on-a-button