how to highlight top row in every excel sheet using Vbscript? - excel

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

Related

AUTOFILTERS in VBA

I thought I was finally done with my workbook but alas there arose an issue when we put it into practice (much to my dismay).
In short;
I was using autofilter (not in vba) menus to filter the items in my warehouse, and the shelves they were on.
When I filtered out everything but the shelves i wanted to add inventory to, the values I added (through a VBA programmed button that basically copies everything in the "add to stock" (AKA "C4:C1000" row and adds it into the "currently in stock" row (AKA "D1:d1000")) got added to the wrong row.
My solution was to use the autofilters to find the correct shelf, write the amount added to the stock, and then press the button.
The VBA code of the button would (in my new plan) then do exactly as before, only this time it would first remove the filters, execute as before, and then re-apply filters.
I cannot - for the life of me - figure out how to turn the autofilters back on with VBA code though.
I have searched far and wide on the net, but the closest I can find to what I want is the following code:
Activesheet.range("a4").autofilter
That does nothing but stop my code from completing its execution mid-way.
Please help!
The full code for one of the pages is as follows:
Sub AddtoInnTotalandclear()
'The macro is used to move all amounts plottet into "INN" colum. Amounts are moved into "TOTAL AMOUNT IN STORAGE" while clearing the "INN" colum simultaneously
'Removes flickering from the screen (part 1 of 2)
Application.ScreenUpdating = False
'removes the protection on the worksheet (NB! you have to have the current password in the code line for this to work)
ActiveSheet.Unprotect Password:="kirk"
'Copies the values from the "inn" colum
Range("c4:c1000").Copy
'Adds the copied values to the values already in the "Total Amount" colum
Range("d4:d1000").PasteSpecial Operation:=xlPasteSpecialOperationAdd
'Clears the "inn" colum
Range("c4:c1000").ClearContents
'Disable marchiing atnsa around copied range
Application.CutCopyMode = False
Range("d4:d1000").Select
Selection.Locked = True
'Allows autofilter usage despite the document being locked
'&
'Re-Activates the password protection
With ActiveSheet
.Protect Password:="kirk", AllowFiltering:=True
.EnableSelection = xlNoRestrictions
End With
'Determines where you end up when you are finished
Worksheets("in").Range("c4").Select
'Removes flickering from the screen (part 2 of 2)
Application.ScreenUpdating = True
End Sub
---- I want to remove/disable autofilters when I press the button that activates this VBA code, then re-activate the autofilters once the entire procedure is done...
I need an EXCEL JEDI to give me some sound code advice here.. Please :)
Here is a demo example. Say we have data in cols A through D with the headers in row#1. If you run:
Sub qwerty()
Dim s As Worksheet
Set s = ActiveSheet
s.AutoFilterMode = False
s.Range("A:D").AutoFilter
End Sub
You will end up with filtering on cols A through D, but with no criteria on any of those columns applied.
EDIT#1:
If your header row is row #3 and we are filtering cols A through D, then:
Sub qwerty2()
Dim s As Worksheet, N As Long
Set s = ActiveSheet
N = Cells(Rows.Count, "A").End(xlUp).Row
s.AutoFilterMode = False
s.Range("A3:D" & N).AutoFilter
End Sub

Copy entire row of active cell to one row down IF row is empty

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.

Excel VBA: Replace Range('Cell).Select with an If Statement

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.

Select range from A1 to end of file using VBA in Excel

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:

Macro to Create combobox in Excel anywhere

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 !"

Resources