I am using Excel Visual Basic for Applications (VBA)
I have a Macro that is selecting a set of cells, cutting them and pasting them into a defined cell in another sheet that the user selects. However, I need to have the macro look for empty rows (represented as a 0) in the user-chosen spreadsheet, as data may already be in the spot where it is getting pasted.
My Code:
Sub Button11_Click()
'
' Button11_Click Macro
'
'
Range("A2:K2").Select
Selection.Cut
Sheets(Application.InputBox("Type desired Team")).Select
Range("B4").Select
Sheets("ImporterSheet").Select
End Sub
Basically,
Range(B4).Select
needs to be replaced with
Range(If ActiveCell <> 0 then ActiveCell.Offset(1,0). If ActiveCell = 0 then paste data here)
Try this:
Range(Range("B:B").Find(0, [B1]).Address).Select
It selects the first cell with 0 in it in Column B.
Is this what you need?
BTW, try going through THIS which is definitely a good read to improve your coding.
Related
Relatively new to VBA, so apologies if this is a poorly phrased question:
How can I set up a macro so that it will copy the entire row containing the active cell to the next empty row down?
Potential complications:
All data is being entered in a pivot table due to other functions
One column auto-calculates, does that count as not being empty?
I managed to make a macro copy to the next row down, but it will
overwrite that row as it stands.
(I know I could copy paste the row but I'm making a very large spreadsheet for other people who want to fill it as fast as possible.)
A brief description of what I have so far:
Sub CopyDown()
'
' CopyDown Macro
' duplicate current row
'
' Keyboard Shortcut: Ctrl+w
'
If IsEmpty(ActiveCell.Offset(1, 0).EntireRow) Then
'This part works
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
ActiveSheet.Paste
'This part works
End If
End Sub
Advice would be appreciated, even if it's just the right keywords to search for this type of function.
I´ve a file with about 50 tabs, and each with a variable name.
Each tab includes a standard form (15 columns, 1000 or more rows).
I need to check each row:
If column D has a non-null value and column B is greater than some other value, I need to bring the value of column A in a different tab.
I´ve to admit I thought this would be simple but I can´t find a way to make it work.
Any advise would be much appreciated!
Based on the detail you've provided, my understanding is that in every worksheet you want to bring forward some value from a base tab. Thus, the following formula would work:
=IF(D2>0,IF(B2>100,Sheet2!A2,"2nd False"),"1st False")
As you copy this down across rows, it will fetch the corresponding value on the same row in another sheet, which as far as I can see is what you're after. If it isn't, then yes you would need to bring in a vlookup.
In terms of automation, either highlight all your tabs or record a macro with the following steps:
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
Range("E2").Select
Selection.Copy
ActiveSheet.Next.Select
Range("E2").Select
ActiveSheet.Paste
Selection.AutoFill Destination:=Range("L2:L288")
Range("E2:E288").Select
Range("E2").Select
Application.Run "'Your File.xlsx'!Macro2"
End Sub
Hope that is what you're after and good luck
I have a Vbscript code that generates multiple sheets in a new excel file. But automatically the last entry of each sheet is highlighted i.e. control resides with the last row. So for every sheet, one has to scroll all the way up. I don't actually want to highlight, I just need the control at the top. How can I set the control in each sheet to the first entry/row? Thanks for your time.
You can adapt this VBA sub to your needs:
Sub JumpAndPosition()
For i = 1 To Worksheets.Count
Worksheets(i).Activate
Range("A1").Select
Application.Goto Reference:=ActiveCell.Address(ReferenceStyle:=xlR1C1), Scroll:=True
Next i
End Sub
' Assuming your Excel object variable is named 'objExcel'...
Dim Sheet
For Each Sheet In objExcel.ActiveWorkbook.Sheets
Sheet.Cells(1, 1).Select
Next
You are using Excel and there is no reason to ask anyone these types of questions because you can record it in Excel.
Alt + T, M, R
then Home key then Up Arrow. Stop recording.
Gee look what Excel wrote
Selection.End(xlUp).Select
or if you had of recorded Go To dialog
Application.Goto Reference:="R1C1"
or if you had of recorded Ctrl + Home
Range("A1").Select
I want to select all values in Excel 2007 worksheet between A1 and end of file (effect of ctrl End). There are always 4 columns but the rows will range from 2 to possibly hundreds. There will possibly be lots of blank cells throughout the selection, including the last cell.
The following just goes to the last cell to be selected, not the entire range. How can I modify this to accomplish what I want?
ActiveSheet.Range("A1", SpecialCells(xlLastCell)).Select
Many thanks.
You almost have it. The SpecialCells method needs a qualifier:
ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
If you always want the first four columns, then perhaps:
Sub dural()
Intersect(ActiveSheet.UsedRange, Range("A:D")).Select
End Sub
Record a macro doing it then review the code:
Something like this may work.
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Running this macro selected the below in my example:
I want to create a Macro that allows comboboxes to be placed in every cell that I select. I am able to create a Macro places the combobox in the same cell, where I record it. It gives it a range, like "N15", so every time I execute the macro, the comboboxes stack up in the same cell. Since i am new in VB, i am requesting if someone can tell me the trick to place it in the active cell.
I have a reference cell link. I found a helpful person in one of the forums, who has supplied a code to "refresh" the links, when you copy-paste. It would be nice to just combine this along with the combobox.
When I add ActiveCell, it gives me an error: "Object doesn't support this property or method".
This the code the macro of a simple copy-paste creates:
Sub AAA()
'
' AAA Macro
'
'
Selection.Copy
ActiveCell.Offset(-3, 1).Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Shapes.Range(Array("ComboBox4")).Select
Application.Run "ExcelTemplate_DayOne.xlsm!Sheet1.Test"
End Sub
The Range("A1") is what will ensure that every time i run the macro, the combobox will go and get added to that same cell. If I change Activesheet.Paste to ActiveCell.Paste, it gives me the above error, I mentioned.
To use the active cell, use ActiveCell
For example, to get the row of the active cell, use : ActiveCell.Row
To get the column : ActiveCell.Column
To put something in that very cell you could either do :
ActiveCell = "SOMETHING!"
OR :
Cells( ActiveCell.Row, ActiveCell.Columns ) = "SOMETHING TOO !"