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
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I am trying to apply a VBA to a workbook with multiple worksheets where sheetnames are variable but fail.
MY code only works on active worksheet.
Could please help me to see what is the problem?
Sub sample_code()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
Range("A1").Value = "test each worksheet"
End With
Next ws
End Sub
Sorry, can't test this right now, but does the following work?
Sub sample_code()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.Range("A1") = "test each worksheet"
' (.Range("A1").Value is okay too)
End With
Next ws
End Sub
You should fully qualify your Cell A1 range - example .Range("A1")
Intro to VBA: The Excel Object Hierarchy ( Jon Acampora )
Look into VBA objects which are organized in a hierarchy so it makes them easier to reference an object
At the top it is the Excel Application. All the objects within Excel are members or sub-members of the Application object.
The dots between each word allow us to reference members of the hierarchy from top down
Remember VBA allows us to make assumptions when referencing objects. If you don’t specify the workbook or worksheet in a line of code, then VBA assumes you are referring to the active workbook and active worksheet.
For example, the following line of code will clear the values and formulas of all the cells on the active worksheet.
Cells.ClearContents
If you don’t tell VBA which sheet in which workbook you want to clear, then this could spell disaster! You can’t undo that action.
So you would want to qualify this line of code to tell VBA which workbook and worksheet you are referring to.
Workbooks("Book1.xlsx").Worksheets("Sheet1").Cells.ClearContents
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
I have just started to learn how to program using Python about a month ago and I am trying to learn a bit about VBA as well. I have an excel document with 3 sheets, the first being an inventory with columns A through W and several thousand rows. The second sheet is the assets that are in question, the third is the destination for the results.
This is the pseudo code for the macro:
Make the second sheet active
Create Loop to go through contents of column A and highlight each
Copy contents of each row to variable one by one
Make the first sheet active
Loop through the contents of Column C and D for aforementioned variable
If found highlight the active row
Copy active row to sheet 3 in next available row starting at A
I have researched how to solve this problem for the last several days, finding code for searching, looping through rows, selecting the appropriate row, going between sheets for the copy command. With all of this I have written what I believe should work for the intended purpose. I have included comments for each line to give my thought process behind it.
The error I'm receiving currently: Run-time error '9': Subscript out of range
Error location: Line 12 - where I set the targetsheet to Sheet(0).
Thank you so much for any help!
Sub SpecialCopy()
Dim targetSh, destinationSh, invSh As Worksheet
Set targetSh = Sheets(1) 'Setting initial value to Page 2 which contains assets being searched for
Set destinationSh = Sheets(2) 'Using a second one for use in the final copy statement to the destination sheet
Dim i As Long
Dim g As Long
Dim asset As String 'Using string as asset row may contain all numbers or numbers and letters
For g = 1 To Cells(Rows.Count, "A").End(xlUp).row 'Using loop to loop through values in column containing assets being searched for
Set targetSh = Sheets(1)
asset = Cells(g, 1).Value 'Setting asset to next value in Sheet 2
Set targetSh = Sheets(0) 'Not sure if I should initialize a third worksheet to use as the worksheet containing inventory, or if setting it twice in the loop would work.
For i = 1 To Cells(Rows.Count, "F").End(xlUp).row 'Looping through values in inventory to find asset
If Cells(i, 3).Value = asset Then
Range(Cells(i, 1), Cells(i, 23)).Copy Destination:=destinationSh.Range("A" & targetSh.Cells(Rows.Count, "A").End(xlUp).row + 1) 'Trying to copy the found asset, including all rows from A to W from Sheet(0) to Sheet(2)
End If
Next i
Next g
End Sub
The code can be found on Github here: https://github.com/cookchelsea/Find_and_Paste/blob/master/Master
The starting point for sheets is Sheet(1), so pointing to Sheet(0) is getting you Run-time error 9, which in this case is because you a referencing a non-existent collection (there is no Sheet(0)). More on that error code here.
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
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
Good afternoon, I am asking this because I didn't get any of the codes I got from the web working. I have a sheet that has a lot of links and I want to be able to open at least 10 of them at a time, in tabs. I previously had some code (that I lost) that opened all of them(in chrome), which was a big problem.
I would like 1 of 2 things:
Option 1: Open all the hyperlinks I select from a column in my default browser(chrome) (the hyperlinks are there with the following formula "=hyperlink(leftcell;"OPEN")
Option 2: I paste either the hyperlink or text in another sheet and the 10 first rows are opened.
I would greatly appreciate the help.
You can use a loop to call the ActiveWorkbook.FollowHyperlink function, and use the links you already have in your worksheet.
Assuming you have the links in the form bellow:
Links in worksheet
You now have to loop in these links and open them one by one. In the bellow code the column A is hard coded, but you can easily change that by another input of yours, like a user selection, for example.
Sub test_link()
Dim current_row As Integer
Dim last_row As Integer
Dim current_sht As Worksheet
current_row = 1
Set current_sht = ActiveWorkbook.ActiveSheet
last_row = current_sht.Cells(current_sht.Rows.Count, "A").End(xlUp).Row
While current_row <= last_row
ActiveWorkbook.FollowHyperlink Address:=current_sht.Range("A" & current_row) 'Open link
current_row = current_row + 1
Wend
End Sub
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I was trying to make a VBA script that would calculate the differene between two cells and place it in the third cell. It should work in the following situation:
Three cells are selected. Two of them have values, the third one is blank.
The VBA script is run.
The VBA script calculates the difference between cells with values.
The difference is recorded in the third (blank) cell.
As you could understand such a VBA script should be able to record the difference in a cell on the right from the values, below, on the left or above.
I'm a newbie in vba so my vba coding depends a lot on the forums were similar issues are discussed. But this time I could not find a solution.
Try this:
Dim rng As Range, cel As Range
Set rng = Selection
If rng.Cells.Count <> 3 Then Exit Sub
With Application.WorksheetFunction
If .CountA(rng) <> 2 Then Exit Sub
For Each cel In rng
If cel.Value = "" Then
Select Case True
Case cel.Address = rng(1).Address
cel.Value = rng(2)-rng(3)
Case cel.Address = rng(2).Address
cel.Value = rng(1)-rng(3)
Case cel.Address = rng(3).Address
cel.Value = rng(1)-rng(2)
End Select
Exit For
End If
Next
End With
not tested soi leave it to you.
Edited for simoco