Display one column at a time - excel

I would like to know if this is possible and how i could do this.
I have a workbook with 2 sheets. Sheet2 has multiple columns with 50 different records. sheet1 i would like to have a play button or run button when i click "play/run" it it will have one column and that one column will display the records of each column from sheet2 until there are no more columns form sheet2. Additionally, the display will have a 5 sec interval before cycling to the next column.
i found out how to do the time interval but not the displaying
'time interval
Application.Wait Now + TimeSerial(0, 0, 5)
'displaying i have been using copy/paste but it does not work.
Thanks in advance

Add these procedures to the VBA module. Then add a button or textbox/etc. to the Sheet1, and right-click the shape, and choose "Assign Macro", then select the MyButtonClick procedure. This will associate the macro with the button.
Then, you just need to loop over the columns and copy/paste like so:
Sub MyButtonClick()
Dim ws1 as Worksheet, ws2 as Worksheet
Dim cols as Range, c as Range
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Set cols = ws2.Range("A1:G50") '## Modify as needed
For Each c in cols.Columns
c.Copy Destination:=ws1.Range("A1") '## Puts the column in Sheet1, Column A
Call WaitForIt(5)
Next
End Sub
Sub WaitForIt(seconds as Long)
Application.Wait Now + TimeSerial(0, 0, seconds)
End Sub

A somewhat minimal-VBA approach:
Say that sheet2 looks like:
then in sheet1, create a 1-cell named range record (cell A2 in this case):
In column B put the formula:
=IF(NOT(ISBLANK(INDIRECT("Sheet2!R"&ROW()&"C"&record,FALSE))),INDIRECT("Sheet2!R"&ROW()&"C"&record,FALSE),"")
and copy it down for as many rows as the longest record in sheet 2
Then -- the VBA part can just have a loop where it has the statement
Range("record").Value = i
(with an i that cycles through the column numbers containing records). The spreadsheet formula takes care of pulling the correct values.

Related

Enter value to last row/first empty cell in a new column IN table using VBA

I have a problem with a vba macro that i can't seem to find the answer to anywhere. Feels like i've tried everyting so i'll put the question out there to see if anyone here can help me :)
My macro loops through 50 woorkbooks that all have a "Firstpage" where the data from all the other data worksheets are summarized. In that Firstpage i have a table called "Tabell_1". The table has a header row (B4:F4) and then one row for each data worksheet in the workbook and the a sum row. We have decided to add a new column (column D) to the table to add in data from a specific cell in all the other worksheets (B4).
I now loop through the data worksheets to copy the value in B4 and then i want to paste that value to the first empty row in the table on the "Firstpage" (starting from the cell under the header). The method to find the last row that i use in other parts of the macro doesn't work, it gives me the first row after the sum row and then pastes the values under the table.
The picture shows the table that i'm working with for one of the workbooks.
enter image description here
Hej Johanna!
Assuming that you are using listobjects to manage the data I would just do this..
Sub Test2()
Dim lo As ListObject
Dim lr As ListRow
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set lo = ws.ListObjects("Tabell_1")
Count = 1
For Each lr In lo.ListRows
If lo.ListColumns("Test2").DataBodyRange(lr.Index) = "" Then
lo.ListColumns("Test2").DataBodyRange(lr.Index) = Count
Count = Count + 1
End If
Next lr
End Sub
This is will fill the first empty row in column Test2.
Hopefully you can use this example :)

Create a hyperlink to another sheet within the same workbook to first non-blank cell

I have a main sheet in which I manage my trades for currency on the FOREX. On that main sheet I have made buttons that I would like to hyperlink to the subsequent sheets that hold the daily data for each specific currency pair.
I am able to create a hyperlink to the sheet itself by using the hyperlink function but I want the hyperlink to actually find the first blank cell in column B and then count up 10 cells so that the last ten cells are visible on the sheet.
I have tried using Indirect, Offset, CountA, and Match combinations and variations to no avail. Please help.Main Screen Shot
Daily Sheet Screenshot
To simplify, you can replicate this code for each button. Change only one line for each button (and the subroutine name, of course)
Sub JPY_Daily_click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("JPY_Daily")
myrow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row - 10
sh.Activate
sh.Range("A" & myrow).Select
End Sub

Update formula when inserting or deleting rows

I have a spreadsheet for measuring costing. All the data is stored within a table. I want to add or delete rows from the table.
Columns A, B and C have data validation lists stored on another tab. Columns D and E are the number of units and number of workers. F, G and H are total rows. F is the unit cost without tax. That is all in a table but there is a formula out in cell M that the formula in cell F makes reference to.
The first row in the table is row 7 and the cell M formula is
=IF(OR(ISBLANK(A7),ISBLANK(B7),ISBLANK(C7)),0,VLOOKUP(CONCATENATE(A7," ",B7," ",C7),Service_Price_List,2,FALSE))
I can't code but I found some code to add and delete a row.
The formulas fill down within the table so it updates the formulas to the correct dynamic formulas in columns F, G and H. The cells within the table update correctly when I add a row but the first time I add a row after performing another action, the cell in column M is added but referencing is wrong.
You might be at row 8, 12 or 13 etc. (any row) but it will put this formula in the cell
=IF(OR(ISBLANK(A7),ISBLANK(B7),ISBLANK(C7)),0,VLOOKUP(CONCATENATE(A7," ",B7," ",C7),Service_Price_List,2,FALSE))
which is the formula from the first cell in the list. If I then try to add another row, (click the add row button twice), the cell will be blank.
As the insert and delete buttons work, I think I need a way to copy the formula from M7, which would be the top of the list. Or would it be better to have the formula inserted dynamically through VBA?
This is the code for my button to add a row:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("table3")
'add a row at the end of the table
tbl.ListRows.Add
End Sub
This is the code for my button to delete a row:
Private Sub CommandButton4_Click()
Dim oLst As ListObject
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=""
If ActiveSheet.ListObjects.Count > 1 Then
For Each oLst In ActiveSheet.ListObjects
With oLst
If .Name = "Table3" Then
If oLst.ListRows.Count > 1 Then
number_of_columns = .ListColumns.Count
oLst.ListRows(oLst.ListRows.Count).Delete
End If
End If
End With
Next
'ActiveSheet.Protect Password:=""
End If
End Sub

VBA: Macro to loop through rows and Autofill

EDIT: If I instead wanted to autofill these cells, would the following code work?
Sub BC_Edit()
' Define width and height of table
Dim datasetWidth, datasetHeight As Integer
' Find values for width and height of table
datasetWidth = Range("A1").End(xlToRight).Column
datasetHeight = Range("A1").End(xlDown).Row
' Loop over each column
For x = 1 To datasetWidth
Set sourceRange = Cells(2,x)
Set fillRange = Range(Cells(3, x), Cells(datasetHeight, x))
sourceRange.AutoFill Destination:=fillRange
Next
End Sub
I'm working with a couple of extremely large datasets - each approximately 3000 rows by 4000 columns. While Excel may not be the best tool for the job, I have already built a significant amount of infrastructure around the data and cannot move to a different framework. I'm using Excel 2007.
In a particular worksheet, when I try to Autofill using a formula I have inputted for the entire second column B (3000 x 1) via copy and paste of this column into the remaining 3000 by 3998 selection, or some part of this selection, Excel gives me a memory/resources error. So I would like to instead loop through each row and Autofill across all the columns. (In other words, I'd like to use A2 to autofill A3:A4000, B2 to autofill B3:B4000, and so on.) Perhaps this would help with the memory issue. How would I go about writing a macro to accomplish this?
I'd appreciate any advice on this issue, and perhaps some help with the appropriate VBA code, if possible.
Here is a pretty basic example of a macro to set columns below 2 to the formula of column 2.
It would be best to have this attached to a button or something similar rather than running every time you open the sheet.
Sub Button_Click()
' Define width and height of table
Dim datasetWidth, datasetHeight As Integer
' Find values for width and height of table
datasetWidth = Range("A1").End(xlToRight).Column
datasetHeight = Range("A1").End(xlDown).Row
' Loop over each column
For x = 1 To datasetWidth
' From row 3 to the height of data, set the formula of the cells to
' the formula contained in row 2 of that column
Range(Cells(3, x), Cells(datasetHeight, x)).Formula = Cells(2, x).Formula
Next
End Sub

vba excel copy subtable from sheet to sheet

I realize that this is probably a duplicate, but I've been searching for an hour and I can't to get the syntax right.
I have a sheet with several tables. There is at least one empty column and one empty row between one table to the other.
I know the start row and start column of each table, and I know that each table has 3 columns. I don't know how many rows it has.
I want to write a sub that receives:
table start row
table start column
and copies the table into another sheet (let's say that the destination is sheet2 starting at A1).
I know I can do it with a loop, but I suspect there is a better syntax right?
(The main issue here is that I need to find the number of rows each table has)
Thanks.
Li
This sub will do the job:
Sub CopyTable(wsSource As Worksheet, lngTopRow As Long, intLeftCol As Integer, rngTarget As Range)
Dim rngSource As Range
Dim intCols As Integer, lngRows As Long
Set rngSource = wsSource.Cells(lngTopRow, intLeftCol)
intCols = rngSource.End(xlToRight).Column - intLeftCol + 1
lngRows = wsSource.Cells(wsSource.Rows.Count, intLeftCol).End(xlUp).Row - lngTopRow + 1
rngSource.Resize(lngRows, intCols).Copy rngTarget
End Sub
To copy a table starting in rows 1, column 5 of worksheet1 to Sheet2!A2, use the following call:
CopyTable Sheets("Sheet1"), 1, 5, Sheets("Sheet2").Range("A2")
This assumes that there's nothing below each table. If that is not the case, replace the 3rd instruction with
lngRows = rngSource.End(xlDown).Row - lngTopRow + 1
This will now assume, that the first column of your table has no gaps. If this is not true, try using rngSource.UsedRange.Row instead of rngSource.End(xlDown).Row!

Resources