Looping through records, printing out to another workbook

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
49
Office Version
  1. 365
  2. 2016
Hey there, I'm looping through an array of values that meet a certain condition (lets say all companies that are in NY). I've done this successfully (can print these out to the immediate window, get the results I want). I'm just curious, what is the simplest way to print out these results to a new workbook? I can print it out to a text file but I want the end user to be able to view the records in an excel spreadsheet. Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
copy cells, paste in new wb.

Code:
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
Thanks RM - so Im looping through a bunch of rows and I just want to print out ones that meet a certain condition(Im printing them to the immediate window now). Is there a simple way to take JUST those records and print them to the new workbook?
 
Upvote 0
i have a solution, but i need to know what kind of conditions you want to enter.
will this be on a form? or excel cells to enter conditions?
 
Upvote 0
change it to fit your data:

code for the FORM at button click

Code:
Private Sub btnFind_Click()
Dim vCol As String
Dim vVal2Find

Set gcolParms = New Collection

'------CHECK  ALL BOX PARAMETERS TO SEARCH
If Not IsNull(txtDate) Then
   vCol = 1
   vVal2Find = txtDate
   GoSub AddParm
End If


If Not IsNull(txtCo) Then
   vCol = 12
   vVal2Find = txtCo
   GoSub AddParm
End If


'----POST RESULTS
If gcolParms.Count = 0 Then
  MsgBox "No parameters given"
Else
  FindData
  Unload Me
End If
Exit Sub

AddParm:
   gcolParms.Add vCol & ":" & vVal2Find
Return
End Sub


code for the search on data
paste into a module

Code:
'multi parameter data search
'written by ranman256

Public gcolParms As Collection
Public Const kRSLT = "Result"
Public Const kFND = "Found"

Public giColRslt As Integer
Public gsColLtrRslt As String
Public mvRet


Public Sub FindData()
Dim i As Integer, j As Integer, iFld As Integer, iCt As Integer
Dim vDate As Date, vStartDte As Date, vEndDte As Date
Dim vVal, vWord
Dim bFoundFinal As Boolean, bFoundCurr As Boolean
Dim rng

 '-------make RESULT field
Range("A1").Select
Selection.End(xlToRight).Select
If ActiveCell.Value <> kRSLT Then
   ActiveCell.Offset(0, 1).Select  'next col
End If

giColRslt = ActiveCell.Column
gsColLtrRslt = getColLtr()
Range(gsColLtrRslt & ":" & gsColLtrRslt).ClearContents
Range(gsColLtrRslt & "1").Value = kRSLT

Set rng = ActiveSheet.UsedRange


''---------scan the data for the SEARCH parameters
Range("A2").Select
While ActiveCell.Value <> ""
   
  ' If ActiveCell.Row = 25 Then   'debug
  ' Beep
  ' End If
   
   iCt = gcolParms.Count
   ReDim ary(iCt, 1)
   For i = 1 To iCt
       vWord = gcolParms(i)
       j = InStr(vWord, ":")
       iFld = Left(vWord, j - 1)
       vVal = Mid(vWord, j + 1)
       
       Select Case True
            Case IsDate(vVal)
               bFoundCurr = ActiveCell.Offset(0, iFld - 1).Value = CDate(vVal)
            Case IsNumeric(vVal)
               bFoundCurr = ActiveCell.Offset(0, iFld - 1).Value = Val(vVal)
            Case Else
               bFoundCurr = ActiveCell.Offset(0, iFld - 1).Value = vVal
       End Select
       
        If Not bFoundCurr Then GoTo nextRow   'param FAIL since we need all of them
   Next   'next param
   
   
       '-----mark found success
    If bFoundCurr Then ActiveCell.Offset(0, giColRslt - 1).Value = kFND           'write the valid entry to a result column. then sum it.
   
   
      '------next data row
nextRow:
   ActiveCell.Offset(1, 0).Select  'next row
   bFoundFinal = False
Wend


 '----filter the results
rng.AutoFilter Field:=giColRslt, Criteria1:=kFND

'-----copy resutls
SaveFoundData

Set rng = Nothing
Set gcolParms = Nothing
End Sub


Private Function getColLtr()
mvRet = Mid(ActiveCell.Address, 2)
getColLtr = Left(mvRet, InStr(mvRet, "$") - 1)
End Function

'---------------------
Private Sub SaveFoundData()
'---------------------
Range("A1").Select
ActiveSheet.UsedRange.Select
     'Range("A1:G27").Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    ActiveWorkbook.SaveAs Filename:="C:\TEMP\found data.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top