Skip to content

Latest commit

 

History

History
94 lines (82 loc) · 2.3 KB

Excel VBA - CSV Class Object.md

File metadata and controls

94 lines (82 loc) · 2.3 KB

Class Object

Focus on the object methods first & along the way, you'll figure out the required object properties.

Properties

  1. f
  2. f
  3. f
  4. f

Methods

  1. m
  2. m
  3. m
  4. m

Method - Import Sheet

Public Sub ImportSheet()
	' Declaring variables
		Dim ws As Worksheet
	    Dim strFile As String, strFilter As String, strCaption As String
		Dim TargetBook As Workbook
	' Define Variables
		Set ws = ActiveSheet
		strFilter = "Text Files (*.prn ; *.txt ; *.csv)"
		strCaption = "Please Select a file to import."
	' this enables user to determine which file they would like to copy to current sheet.
		strFile = Application.GetOpenFilename(strFilter, , StrCaption)
			If strFile = "" Then Exit Sub
		Set TargetBook = Workbooks.Open(strFile)
	' Procedure starts here
		TargetBook.Sheets(1).Copy After:=ws
		TargetBook.Close SaveChanges:=False
	'Clearing VBA Memory
		Set ws = Nothing
		Set TargetBook = Nothing
		strFile = vbNull
		strFilter = vbNull
		strCaption = vbNull
End Sub

Method - Import to Range

Public Sub ImportRange(Optional ByRef vImportToRange As Range)	
	Dim rng As Range
	Dim TargetBook As Workbook, TargetRange As Range
	Dim strFile As String, strFilter As String, strCaption As String

	If vImportToRange = Nothing Then
		With ActiveWorkbook.Activesheet
			Set rng = .Activecell
		End With
	Else
		Set rng = vImportToRange
	End If
		strFilter = "Text Files (*.prn ; *.txt ; *.csv)"
		strCaption = "Please Select the CSV file to import."
	' this enables user to determine which file they would like to copy to current sheet.
		strFile = Application.GetOpenFilename(strFilter, , strCaption)
			If strFile = "" Then Exit Sub
		Set TargetBook = Workbooks.Open(strFile)
		Set TargetRange = TargetBook.Sheets(1).UsedRange
	' Copy the value from CSV file to the target range
			rng.value = TargetRange.Value
			TargetBook.Close SaveChanges:=False	
	'Clearing VBA Memory
		Set rng = Nothing
		Set TargetRange = Nothing
		Set TargetBook = Nothing
		strFile = vbNull
		strFilter = vbNull
		strCaption = vbNull
End Sub

Return

Basic Module

'*****************************************************
' Purpose: 
' Inputs: 
' Returns:  
Public Sub SomeSubroutineName()
	
	' Write code here'
	
End Sub

created on 2022-11-18 at 20:51 #VBA