473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VBA (Excel) to select random records from multiple tabs and paste them in another tab

newnewbie
I am learning VBA and need a little help with a macro that I am writing.
I have a worksheet with data that I split into multiple worksheets based on criteria in one column. E.g. Column A has unique usernames, I have a macro (found it online and tweaked) that splits the spreadsheet (Sheet1) into different tabs named by the criteria (lgold, gsmth, ppatty, etc.). So, as a starting point I have an excel file with 20-30 tabs. all named differently, with data. What I need done firther is:

1. delete tabs with fewer than 31 rows
2. select 5 rows from each remaining tab and copy them to a new tab, named “To Audit”. The 5 rows from each tab should not be the first 5, but either
a. random 5 rows, or
b. every Nth row….e.g. if “lgold” tab has 346 rows =346/5=69.2=select every 69th row and copy it to a new tab....whichever is easier

I need a push in the right direction as far as sequence and which functions to use. I am at the first few chapters of the “idiot’s guide to VBA” level right now….the above macro is a little too advanced for me to write yet.

Thank you all for help!

Lena
Nov 7 '08 #1
2 5878
You can get the bottom row from a particular column on a particular sheet by calling this function...
'--
Expand|Select|Wrap|Line Numbers
  1. .Function GetTheBottomRow(ByRef WS As Excel.Worksheet, Optional ByRef lngCol As Long) As Long
  2. If lngCol = 0 Then lngCol = 1
  3. GetTheBottomRow = WS.Cells(WS.Rows.Count, lngCol).End(xlUp).Row
  4. End Function
'--
The Rnd function returns random numbers.
'--
You can add data from a row on one sheet to another sheet this way...
Worksheets("Sludge").Rows(1).Value = Worksheets("Mush").Rows(13).Value
Nov 7 '08 #2
If you have not completed your task, reply, and I will assist you with your code. I write quite a bit of VBA, and this shouldn't be too difficult. The following code will step through each sheet in a workbook, deleting all sheets with less than data through column 5. Keeping your data consistent on the user sheets will be the most important part of the task. If the user data starts on row 5 with only one row of data, the sheet will be retained.

Good luck with your project


Sub testing()

Dim Wsht As Worksheet
Dim X As Long

For Each Wsht In Worksheets
X = GetTheBottomRow(Wsht)
Wsht.Select
If X > 4 Then
MsgBox ("lets keep this one")
' insert your code to randomize the the selection of rows
' move the rows to your evauluation sheet and evaluate
Else
MsgBox ("lets dump this one")
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End If
Next

End Sub
Nov 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
15
by: daniroy | last post by:
Hello everybody, your help will be very very welcome on a very classic question... I have some Excell Macro background, a little bit of VBA knowledge. I import daily from Internet series of...
13
by: Arno R | last post by:
Hi all, I will have to handle a lot of really 'nice' data in a LOT of Excel sheets. It is all about music files (Billboard top 100) I am afraid there really is a sheet for every year ... (Don't...
2
by: TM | last post by:
I have an Excel sheet where I setup my needed formatting, page settings, ect, and would like to take my data from a datagrid and paste it into the excel file and print the excel file. Any idea...
2
by: Questman | last post by:
Good afternoon, Does anyone have any code that implements, or approaches implementing, a cross-browser DHTML/JS solution to provide an Excel-like Grid on a web page - I'm trying to convert an...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
0
by: grego9 | last post by:
I have a bit of visual basic code in an excel spreadsheet that I need some help with. I am attempting to search a file called TO Cancellations2.xls for counterparty names and for each counterparty...
2
by: grego9 | last post by:
-------------------------------------------------------------------------------- I have a bit of visual basic code in an excel spreadsheet that I need some help with. I am attempting to search a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.