mesaage box displaying values from multiple columns in excel vba - excel

There are three columns. one has some names of tables and second column has its free space and third has percentage used.
help required is:
Msgbox should display the value of PERCENTAGE USED column in the cell it finds more than 90.
Also it should display the name of the corresponding table and the free space of the table for which it is displaying the value in step one.
msgbox output should be:
"ABCD table is 96.67% used and space left is: 7431MB "
Sample Code which displays only table name and percentage used:
Dim rngValues As Range
Dim strTableName As String
Dim cell As Range
' Adjust ranges to suit
Set rngValues = [JI3:JI90]
For Each cell In rngValues
If cell.Value > 94.45 Then
cell.Select
MsgBox Range(cell.Address).Offset(0, -268).Value _
& " table has current size: " & cell.Value
cell.Interior.Color = RGB(255, 0, 0)
End If
Sample output of this code : ABCD table has current size : 97.87%

One way to solve this problem is to use a second Range(cell.Address).Offset().Value statement to append the additional information to the text being displayed by the message box. The exact code is going to depend on how many columns away from column 'JI' the column showing the free space left is.
Assuming that it is the column before the one containing the % space used you could do the following:
MsgBox Range(cell.Address).Offset(0, -268).Value _
& " table has current size: " & cell.Value _
& " and the space left is: " & Range(cell.Address).Offset(0, -1).Value
If it isn't the column before the one containing the % space used you simply need to adjust the column offset in Range(cell.Address).Offset(0,-1).Value properly. For example if the column containing free space occurrs 9 columns after column JI change the Offset to Offset(0,9)
The first argument between the parentheses for Offset is the number of rows and the second one is the number of columns. negative offsets specify rows or columns appearing before the selected cell, postivive specify rows or columns appearing after the selected cell. '0' specifies the current row or column.

Related

Cut and paste using an offset

I am building a web scraping tool that obtains particular data. Once the data has been extracted the next step is to summarize it into a report thus i need some guidance on the final part of the project.
I have a column (Column A) that contains the following data set
Description of product
$3000
Description of product
$5000
etc
I would like to find a value (in this case the common value is $) and cut this value next to the description (into Column B). There could be hundreds of rows thus a loop would be required.
My initial thought is to use code that will find a value ($), then once the value is found, cut the row and using an offset paste the value (into column B)
Any help would be appreciated
sub test()
dim usedrows,i as integer
usedrows = activesheet.range("A" & activesheet.rows.count).end(xlup).row
for i=0 to usedrows
if instr(range("A" & i+1),"$") <> 0 then
'Checks if the looped cell has "$" sign
range("B" & i+1)=range("A" & i+1)
range("A" & i+1)=""
end if
next
end sub
Copy ColumnA into ColumB. Delete B1 with Shift cells up. Series fil1 a column with 1 in odd rows, 2 in even, then filter to select the 2s delete those rows and then the column of 1s.

Returning multiple comma delineated column header values in excel

My dataset:
I've got data sorted like this, however, I'd like to have a single cell that displays data like any of the below options. I've listed them in order of preference on my part.
UT (4), CO (2), NY (1) --- basically the column header with the value in parenthesis in decending order (most ideal)
UT, CO, NY --- same idea without the value count, still decending order
CO, NY, UT -- basically just a list.
So far I have two formulas that only return the first column.
Formula 1:
=IF(COUNTA($U2:$BS2)=0,"",INDEX($U$1:$BS$1,MATCH(FALSE,INDEX(ISBLANK($U2:$BS2),0),0)))
Formula 2:
=IF(COUNTA($U2:$BS2)=0,"",INDEX($U$1:$BS$1,MATCH(TRUE,INDEX($U2:$BS2<>"",0),0)))
Any suggestions on what I can do?
Edit - adding VBA function to accomplish this without having to sort the data:
Function StateSort(range As range)
maxValue = 0
'Find max value in row
For Each cell In range
If cell.Value > maxValue Then maxValue = cell.Value
Next cell
StateSort = ""
'Loop backwards from max value down to 0
Do While maxValue >= 0
For Each cell In range
If cell.Value <> "" Then
If cell.Value = maxValue Then
'If your state labels aren't in row 1, change the
'Cells(<row number>, cell.Column) portion below with the row number
StateSort = StateSort & Cells(1, cell.Column) & " (" & cell.Value & "), "
End If
End If
Next cell
maxValue = maxValue - 1
Loop
End Function
The way you use this is right-click on the Sheet tab on the bottom and click "View Code", which will open the code editor. In the menu, go to Insert > Module. Then paste in this code in the blank space and save the workbook (as macro-enabled so the script will run).
Now you can use =StateSort(<range>) (pass in the range of the values) and it will format your data.
Old answer (leaving as an example of sorting by row)
Turns out you can sort by row if you go to Sort > Options... > Sort left to right
So you could put the sum for each of states in a separate row. Then you'd just sort by the sums, and use the CONCAT function to make the headers look correct. ex: =CONCAT("UT (", A4, ")") where A4 is the cell with the sum for the state will give you UT (4).
Full example:
Here's the initial data with the sum and label rows:
Here you can see the formulas in rows 7 and 8:
Then select the area you want to sort and go to Sort > Options... Sort left to right. Select Row 7 (in this example) to sort by:
And your final sorted data:

Trying to find the next instance of exact string in cells

I need assistance finding the next instance of an exact string within a cell.
To be precise, I want to look through a series of headings and find the next instance of a declared variable to obtain the column number, and I want to look through that series of headings to find the next empty cell and save that number, and finally, I'd like to take that first column number, and search from second row until I find the first instance of an empty cell, and save that number into a variable. What I have been doing is this:
With Rows(1)
Set found = .Find(what:=Target, After:=.Cells(1, 1))
End With
But it seems that if I accidentally type "s" it will find the first instance of a cell that contains a string that contains the substring "s" (LastName), and not the first cell that contains only "s".
my fear is that if there are columns with " " in them then my program will not function correctly.
Besides that, I sort by a column and when a cell in that column is empty my program pushes it all the way to the bottom of the list and I am trying to delete that empty cell space.
I tried doing Application.WorksheetFunction.Match, HLookup and VLookup and in general the worksheet functions aren't working for me.
So just to give an example of what I want to do:
I have 10 Columns with headings. I want to find the first instance of a column that
contains exactly the string I send into this class. For instance, if the
columns are "FirstName | LastName | Name", I want it to return "Name"
and not "FirstName".
I want to find a column that the user requests as a sort key and verify it's existence
I also want to find a column that is empty (last column)
Finally, I want to find the last row that has a value in relation to the SortColumn.
If you set the lookat parameter to xlWhole, it will only match the whole contents of the cell, so for example:
With Rows(1)
Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole)
End With
To check whether a value was found, you can check whether found is nothing.
Dim exists As Boolean
If Not found Is Nothing Then exists = True
To locate the first empty cell at the end of a row or column of values, I would use the End property to find the last cell in the row/column containing data, then use Offset to find the next cell:
With Rows(1)
Set found = .Find(what:=target, After:=.Cells(1, 1), lookat:=xlWhole)
End With
Dim emptyCell As Range
If Not found Is Nothing Then
Dim col As Integer
col = found.Column
Set emptyCell = Columns(col).End(xlDown)
emptyCell.Offset(1, 0).Select
End If
However, you can't use this if there are some empty cells in the middle of your table of values. (eg if you have values in A1,A2,A3, then A4 is blank and you have more values in A5,A6,A7).
You can use a do loop:
headerToFind = "Name" 'or whatever header you're looking for
x = 1 'or whatever header row is
y = 1 'or whatever first column with header is
Do Until Cells(x,y) = ""
If Cells(x,y) = headerToFind then
MsgBox "The header you are looking for is in row " & x & ", column " & y
Exit Sub
End If
y = y + 1
Loop
MsgBox "Header not found"
In place of the message boxes, put whatever code you want to do with what you find. The first MsgBox will execute if and when the header is found (with x being equal to the row number and y being the column number). The second MsgBox will execute if the desired header is not found.

Excel VBA count cells until a date is found

I've got an amount of data copied from a table in a .pdf that when pasted into excel puts it all into one column. There are actually multiple pages each with it's own table (the data is one continuous long table split over multiple pages more accurately) and at the top of each page is a series of lines that I'm not interested in (the same unwanted data is at the top of each page). What I am interested in is re-sorting the data under the headers as it is in the table on the original .pdf document, removing the headers in the process. The data as it has been pasted into one column essentially is a list of items in plain text for x rows, followed by a list of start dates for x rows, and then a list of end dates for x rows, repeated every page.
I've figured out how to count the number of lines I don't want by getting a macro to look for the first piece of data I'm interested in ("AAAA") starting at cell (B2);
Cells(2, 2).Select
For i = 1 To 50
If ActiveCell = "AAAA" Then
Exit For
End If
ActiveCell.Offset(1, 0).Select
Next i
Cells(2, 3) = i
If i = 51 Then
Range("B3") = "Cannot find data"
End If
Which starts a search at cell (B2) looking downwards until it finds "AAAA" it then prints how many rows it has moved downwards to find it in cell (C2).
I now wish to be able to start at the cell it has just found [(B34) in this case] and count downwards until it finds the first cell containing a date.
Ultimately I'll need to then count down the same number of cells to find the associated end date and print them all in one row, continuing for the entire column of data.
If anybody could help me with being able to start at the first cell "AAAA" and then count downwards until a date is found, that would be really helpful.
My biggest challeng is to understand what you want to be true. I tryed to make a list of the things what you want.
You have a PDF that when paste in Excel it transform all the
document in one column.
There is a header in each of the Excel pages that you want to delete.
After you find a header you want to find two dates, and they have the same distance from the header.
How I would do it:
For iCounter = 1 to Cells(1048576, 1).End(xlUp).Row
If Cells(iCounter,1) = "YOUR HEADER HERE" then
For kCounter = iCounter to Cells(1048576, 1).End(xlUp).Row
If IsDate(Cells(kCounter,1)) = true then
initialDate = Cells(kCounter,1)
endDate = Cells(2*kCounter-iCounter,1)
End if
Next kCounter
End if
Next iCounter
The following piece of code starts in cell A1 and searches downward until it finds a cell containing a date value. The code only searches until it reaches the last record in the first column (to avoid searching all the way down to the bottom of the sheet if no date is found).
Sub FindFirstDate()
Dim i As Long
For i = 1 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If IsDate(ActiveSheet.Cells(i, 1).Value) = True Then Exit For
Next i
MsgBox "The first cell with a date is " & ActiveSheet.Cells(i, 1).Address
End Sub
In this example the address of the cell with the first date in returned in a MsgBox.

Using the cell value as the cell range for calculation

I am new to VBA. I am presently working on a validation tool already built by my predecessor.
The issue I want clarification on is selecting the cell range based on the result of a formula in another cell.
For example,
I need to find the standard deviation of 800 number among the 1000 numbers of a column say Column B.
I use the formula STDEV.S(), with the range of two cells in the bracket. However, the range of two cells is varying, as the user can select and change the range. The selection of the range by user is printed in a cell, say Upper Limit in C16 and lower Limit in C15. Consider C16 has 950 and C16 has 150 value in them, in that case, how do I insert the formula for stdev.s().?
Thanks in advance for the help.
Assuming that your Data is in Column B and your bounds are in C15 and C16
Sub PasteAFormula()
Dim strFormulaToPaste As String
strFormulaToPaste = "B" & Range("C15").Value & ":B" & Range("C16").Value
strFormulaToPaste = "=STDEV.S(" & strFormulaToPaste & ")"
MsgBox strFormulaToPaste
End Sub
You can then put the contents of strFormulaToPaste into a cell.

Resources