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 6 years ago.
Improve this question
I have a VBA Macro that is working with a very large dataset.
In my dataset I have ~44000 rows. I want to count this within the macro and have tried to use the top methods shown here.
I take variable sncountmax and make it equal to one of the methods in the link above.
sncountmax = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
Even though the sheet contains 44000 rows sncountmax will remain set to whatever it was previously set to.
EDIT: I have since checked and no other macro functions are executing on this sheet. I tried to remove duplicates using the macro and this did not work, but removing the duplicates in Excel did.
Try changing to the following:
sncountmax = Cells(Rows.Count, "A").End(xlUp).Row
This will give the last row in Column A, so in your situation scountmax = 44000
i wouldn't advise using find. Its not reliable as for sometimes it can return nothing depending on Data, and it's slower.
If your sole purpose is counting the lines in the first column :
with thisworkbook.sheets("Sheet1")
sncountmax = .Cells(.Rows.Count, 1).End(xlUp).Row
end with
Reference the sheets and workbooks like the above example.
You might want to use Option Explicit on the top of your module.
Also, i can only guess, but you should remove your Òn Error Resume Next.
Errors are meaning your code is wrong somewhere.
Related
This question already has answers here:
Excel VBA Project has generated multiple Workbook objects
(7 answers)
Closed 3 years ago.
I am trying to get a count of worksheets in the active workbook. When I run Application.Sheets.Count the value I get is one off from what I expected I should be getting 76 and I get 75. In the project explorer I found a strange sheet labeled ThisWorkbook (See Sheet 52 in the image below). I would really like to understand this behavior as it has an impact on my later work. Any insight would be greatly appreciated.
Private Sub UserForm_Initialize()
Dim wsCnt As Long
wsCnt = Application.Sheets.Count
MsgBox wsCnt
End Sub
Edit: In light of some of the contributions below, I offer the following to further clarify what I'm seeing and to provide greater insight into why it is important within this context.
The above image is now a more complete picture of what I'm seeing. Note that ThisWorkbook is at the bottom of the Project Explorer window. For some reason however, sheet 52 is also called ThisWorkbook.
This is contextually important because this value is later used to increment through all created sheets, the number of which will change over time. Since Sheet52 increments the sheet count but not the number of sheets, I'm coming up short by one. I could conceivably introduce a workaround by just adding one, then checking to ensure that the sheet value is not equal to 52 on each iteration of the loop but in addition to being an inelegant approach this may also cause issues in the future if the anomaly crops up again. I would really like to understand this behavior. I haven't found anything in the documentation to indicate that this sheet needs to be created and I haven't seen a ThisWorkbook sheet being created before like the case of Sheet52 nor can I fathom any reason why it would need to be created. I defer to the collective wisdom of the community.
Edit 2: Concerning the duplicate question
The suggestion that this question is a duplicate emanates from my curiosity as to what was happening to generate Sheet52. The question however, pertains to getting the wrong sheet count, which is not addressed in that post. This information was instructive in some regards and did ultimately get me closer to the answer.
As #BigBen mentions, look further into the documentation on ThisWorkbook.
Modified code should work out:
Private Sub UserForm_Initialize()
Dim wsCnt As Long
wsCnt = ActiveWorkbook.Worksheets.Count
MsgBox wsCnt
End sub
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 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 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 6 years ago.
Improve this question
I am creating a program in which everytime it generates, the generated data will appear in a specific sheet and arranged in specific date order . It looks like this.
But when I generate again, the existing data will be replaced/covered by the new data generated. Looks like this.
Now my idea is, before I am going to paste another data, I want to find out first what is the last used cell with value so that in the next generation of data, it wont be replaced/covered by the new one. Can someone help me with this. The output should look like this.
Any help everyone? Thanks!
As per your case try with below code
Dim lastcolumn as Long
lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column + 2
This will assign the value of the last Row (as Integer) to the variable 'lastRow'
Dim lastRow as Integer
lastRow = ThisWorkbook.ActiveSheet.Cells.Find("*", ThisWorkbook.ActiveSheet.Cells(1, 1) _
, , , xlByRows, xlPrevious).row
You could also remove the '.row' at the end of the statement and set it to a range-variable. Then you have more information about where the last row is, but I think in your case it is enough just to know in which row (as Integer) the last used cell is.
If you need the last Column, just replace '.row' at the end of the statement with '.Column'.
EDIT: You also could use ThisWorkbook.ActiveSheet.UsedRange.Rows.Count but i don't like this way, because it don't give you the right result all the time (don't know why, but often had this problem).
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 8 years ago.
Improve this question
I would like to search for a value in a cell in a spreadsheet and select it. Here is some pseudocode for what I want to do:
search for the unique value "Justin123" in spreadsheet
select the cell that contains the value "Justin123"
offset 2 cells to the left of the searched value and replace with new value "John123"
I tried to google a solution but all I found were recommendations on using the record macro feature to search and replace an item. However, what I need to do is slightly different since I need to search and replace the item two units to the left of the searched cell.
See if you understand this code (hope it gets you interested in learning VBA):
Sub TestReplace()
ActiveSheet.Cells.Find(What:="Justin123", LookAt:=xlPart).Offset(0,-2).Value = "John123"
End Sub
The Macro Recorder is a great tool for beginners. Please do use it for simple cells operations.
Basically, what you have to do is initialise a RANGE object (which represents a cell in excel). Then you set the range object to the output of the function .Find(), which returns a range object from the sheet that matches the search inputs. If there is no match, it sets the range object to Nothing. You can then use the method .Offset() that range objects have to traverse across the other range objects in the sheet.
Sub Main()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:= "Justin123", LookAt:= xlPart)
' Check if rng is not Nothing [ie a match was made using .Find()]
If Not rng Is Nothing Then
rng.Offset(0, -2).Value = "John123"
End If
End Sub