Finding the last row with data using vba [closed] - vba

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).

Related

Open multiple hyperlinks in default browser [closed]

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

Excel Row Counting Not Working [closed]

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.

How to search for an item and select it in VBA [closed]

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

Using a Dashboard to copy particular rows in a particular columns and pasting them into a different excel document [closed]

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
In my excel spreadsheet I have a master which basically controls everything. In this master I have a page which allows me to select an xlsx file, then in a drop down it allows me to select which sheet in that chosen file that I want to use. [This part is working perfectly however...]
What i am struggling with is the following, The user must be able to stipulate which row the data starts and which row the data end and what column this data is in
example:
Row in which data starts 7
Row in which data ends 25
Column of the data G
Column of the Data code D
Using this information, i need excel to extract that data and copy it to another spreadsheet that the user has selected and merge them together
Column to input data H
Title of column (the code must name it with the input of the user) TITLE
Column of the data code E
When merging the data it must match the data to the "Data Code"
Thanks in advance
It sound as though you are asking us to design your program.
The user must be able to stipulate which row the data starts and which row the data end and what column this data is in. Example: "Row in which data starts 7 Row in which data ends 25 Column of the data G Column of the Data code D.
Another spreadsheet (workbook?) that the user has selected.
Only you know what your users will find convenient and what will match your existing code. Below I show one method of selecting a range that you might like.
Option Explicit
Sub Test()
Dim CopyRange As Range
Dim reply As Long
Do While True
Err.Clear
On Error Resume Next
Set CopyRange = Application.InputBox(Prompt:="Select ranges to be copied", _
Type:=8)
On Error GoTo 0
If CopyRange Is Nothing Then
reply = MsgBox(Prompt:="Do you wish to exit without copying a range?", _
Buttons:=vbYesNo)
If reply = vbYes Then
' User wants to exit
Exit Sub
End If
' Loop for another go
Else
' User had entered a valid range
Exit Do
End If
Loop
Debug.Print CopyRange.Address
End Sub
You tell us that you have already opened another workbook so you know how to work across multiple workbooks.
The easiest command, in my view, for copying data is:
SourceRange.Copy Destination:=TopLeftCellOfDestinationRange
The above should give you a start on the next sections of your macro. If you have problems come back with specific questions about code that does not work as you require. Please don't provide a list of vague requirements.

How to determine the last column(last column with value) using VBA in excel?

I have a question wonder if anyone can help me out.
I'm doing a project that requires to get a summary of tests result. My flow is as follow,
First, I would need to open my collected test results, then creating a pivot table, after setting accordingly to what i need to view, i will copy the table into another spreadsheet. Over at this spreadsheet, it will change some of the names to the required names and finally, it will be copied to the summary sheet. Then i will need to tabulate the summary in % also.
I'm facing with an issue here which is, for my collected test results, when i put it into the pivot table, i cant determine the number of columns that will appear. For example
In on example, we can have
In another case, we can have
If u notice the second image has more columns used.
Therefore, I was wondering if there is any way that you guys can share with me to determine the last available column with details to be copied.
And can i get the number of the last column so that i can get the summary as well since i need to put it in the form of %.
At the moment, i'm using "Range(A1:Z1000000)" which is quite impossible for me to do anything as i cant really find the % manually.
btw, i need it in VBA code.
Will appreciate for all the opinions and options provided! Thank you in advance!
This should do the trick:
Sub test()
Dim maxColumn As Long
'Create the table here
maxColumn = getMaxUsedColumnNumber("PivotTableSheet")
'do what you need with the number
End Sub
Function getMaxUsedColumnNumber(sheetName As String) As Long
With Sheets(sheetName)
getMaxUsedColumnNumber = .Cells.Find(What:="*", after:=.Range("A1"), LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End With
End Function
You can use .SpecialCells() as follows:
ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
Alternatively, take a look at the ColumnRange property of the PivotTable object. It has a Count property which will return the number of columns in the Pivot Table which could also help.