Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to run a loop in multiple sheets by getting the last column for each sheet. How can I get the last column of every sheet? I tried putting this a function. Can you please suggest on this as I am just a beginner on this.
Update:
Its working fine now. Tried the code provided by #sktneer. Thanks everyone for your inputs.
You may try something like this...
Sub LastColumnInEachSheet()
Dim ws As Worksheet
Dim lc As Long
For Each ws In ThisWorkbook.Sheets
lc = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Debug.Print ws.Name, lc
Next ws
End Sub
Below code will go through all worksheets in the workbook:
Dim oWS As Worksheet
For Each oWS In ThisWorkbook.Worksheets
oWS.Name ' Give you the sheet name
oWS.Cells(1, oWS.Columns.count).End(xlToLeft).Column ' Gives you total columns in a sheet
Next
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have around 500 files of excel and the most part have filter. Now i need to put all information in one sheet. I found how to put all in one sheet but the books with filter only copy information with filter and i need all.
So, i need to remove filter of all excel files. Important (not all files have filter)
Thanks for your help ;)
Before you copy the data, you should use:
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
This will turn off the filter.
Will remove any filter on all opened workbooks, have fun :)
sub try_this()
dim wb as workbook
dim sh as worksheet
for each wb in workbooks
for each sh in wb.worksheets
sh.AutoFilterMode = False
next
next
end sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am very new to VBA and I am seeking help to solve the following problem. I need to multiply a range by a single number. All of the cells with in that range need to be multiplied by that one number.
Thank you!
As what I comment just now.
1.Enter the formula at the cells (e.g. G1)
2.Press Enter key and drag the formula
keong has already shown you one method but unfortunately that method requires one to do the calculation in another cell/range. If that is what you want then go with keong's answer but if you want to do the calculation in the same range then continue reading below.
Here is another method which doesn't use formulas or VBA.
Let's say the range is A1:A10 and you want to multiply the entire range by 5
Simply type 5 in any blank cell. You can delete that later.
Copy that cell
Select Range A1:A10
Right click on it
Click on Paste Special | Values - Multiply as shown below and you are done.
Before
After
Followup from comments
In case you do not want to use a temp cell to write 5 then you can directly set 5 in the clipboard and use it like this.
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim MyData As New DataObject
Dim rng As Range
Set ws = Sheet1
Set rng = ws.Range("A1:A5")
'~~> Put the number 5 in clipboard
MyData.SetText 5
MyData.PutInClipboard
'~~> Get the data from clipboard
MyData.GetFromClipboard
rng.Formula = Application.Evaluate("=" & _
rng.Address & _
"*" & _
Val(MyData.GetText))
End Sub
Like I mentioned, you don't need VBA for this but if you still want to use VBA then you can use this instead of copying the data to the clipboard.
rng.Formula = Application.Evaluate("=" & rng.Address & "*" & MYNUMBER)
Where MYNUMBER is the variable which has the number that you want to multiply the range with.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hi my problem is when the code searches (i, 1) to find "Hello" it finds it but won't send it to WSS and the more I look the frustrated I get can someone help please
Sub AddSelection()
Dim WSD As Worksheet ' Sheet 1 as prices sheet
Dim WSW As Worksheet ' Workings sheet as Information
Dim WSS As Worksheet ' Selections worksheet
Set WSD = Worksheets("Selection")
Set WSW = Worksheets("Workings")
Set WSS = Worksheets("Selections")
' Loop through records on WSD column A
FinalRow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To FinalRow
If WSD.Cells(i, 1) = "Hello" Then
' When I run the code this where the problem is
WSD.Cells(i, 1).Copy Destination:=WSS.Cells(NextRow, 4)
NextRow = NextRow + 2
FinalRow = WSS.Cells(Rows.Count, 1).End(xlUp).Row
End If
Next i
'Make sure WSR is the active sheet
WSS.Select
' Report that the macro is done
MsgBox prompt:=NextRow - 1 & " Results Records Are Copied To Worksheet."
End Sub
NextRow is never defined. You need to define it before using it.
To stop making this mistake, write this at the top of every "file" in your VBA project:
Option Explicit
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to learn how to copy cells from an already open excel workbook to another file. I don't just want a code, I want a tutorial. The problem I have is Google is only giving me forums. Can someone suggest a good tutorial with a video? A hyper link would help.
Option Explicit
Sub CopyFromOpenWorkbookToAnother()
'declare variables
Dim wkbCopy as Workbook, wkbPaste as Workbook
Dim wksCopy as Worksheet, wksPaste as Worksheet
Dim rngCopy as Range
'set variables -> change the names to suit your data needs
Set wkbCopy = Workbooks("WorkbookToCopyFrom.xls")
Set wkbPaste = Workbooks("WorkbookToCopyTo.xls")
Set wksCopy = wkbCopy.Sheets("SheetToCopy")
Set wksPaste = wkbPaste.Sheets("SheetToPaste")
Set rngCopy = wksCopy.Range("A1:A10") 'adjust to whatever range you need
rngCopy.Copy wksPaste.Range("A1") 'adjust to wherever you want to paste
End Sub
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I currently have a vlookup populating row 1 (cells G1-AZ1) with titles, and would like to hide the Columns(G1-AZ1) if the vlookup pulls back nothing/#N/A. I know this is a simple macro but I'am new to VBA and I have had zero luck searching the web.
Thanks!
I usually place such formulas in ISNA() and then just use Excel filter to hide empty rows
=IF(ISNA(VLOOKUP(A3,G1:H7,2,FALSE)),"",VLOOKUP(A3,G1:H7,2,FALSE))
Try this:
Loop throught he header cells
Set the EntireColumn.Hidden property based on your criteria
Use .ScreenUpdating = False to prevent screen flicker and speed it up
Sub HideColumns()
Dim rng As Range
Dim cl As Range
Application.ScreenUpdating = False
Set rng = [G1:AZ1]
For Each cl In rng
If IsError(cl) Then
cl.EntireColumn.Hidden = cl = CVErr(xlErrNA)
Else
cl.EntireColumn.Hidden = cl = ""
End If
Next
Application.ScreenUpdating = True
End Sub