• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Looking to increase Macro efficiency|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Looking to increase Macro efficiency|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosLooking to increase Macro efficienc…
sp_PrintTopic sp_TopicIcon
Looking to increase Macro efficiency
Avatar
adrian@utas
University of Tasmania
Member
Members
Level 0
Forum Posts: 16
Member Since:
July 13, 2016
sp_UserOfflineSmall Offline
1
July 30, 2021 - 1:21 pm
sp_Permalink sp_Print

I have a data set out of our timetabling system (at University of Tasmania) where the weeks that a class is taught for come out in a very unhelpful mix of comma separated values, and ranges with hyphens. Like this:
Example Teaching Weeks Patterns
11,12,13-16,17-21 OR
9-17,18-27 OR
2,3,4,5,6,8-9

The attached macro was designed to look at all of these values, and go through and replace the ranges with comma separated standalone weeks.
It works nicely, but is too inefficient. I have attempted to speed it up, but the 1300+ possible combinations (and the need to check for all of them) is slowing things down. Does anyone have any thoughts looking at the attached, to see if it could be improved in terms of processor demand?

Sub ReplaceHyphens()
'
' ReplaceHyphens Macro
' Used to replace week ranges with specific week numbers
'

'
Dim strCol As String
Dim myRange As String
strCol = InputBox("Please specify the column to be adjusted.... then go and make a coffee (it can take up to 8 min to process!)")
If strCol = "" Then
MsgBox "You didn't specify a column!", vbCritical
Exit Sub
End If

myRange = strCol & ":" & strCol
For i = 1 To 1327

Worksheets("Sheet1").Range(myRange).Select

row1 = Selection.Row
maxrow = Cells(Rows.Count, 1).End(xlUp).Row
numcol = Selection.Columns.Count
col1 = Selection.Column
maxcol = col1 + numcol - 1
For r = row1 To maxrow
For c = col1 To maxcol
cellval = Cells(r, c).Value
If cellval <> "" Then
If Left(cellval, 1) <> "," Then cellval = "," & cellval & ","
Cells(r, c).Formula = cellval
End If
Next c
Next r

Selection.Replace What:=Worksheets("Ctrl+Shift+R to replace hyphens").Cells(i, 1).Value, Replacement:=Worksheets("Ctrl+Shift+R to replace hyphens").Cells(i, 2).Value, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False

Next i

Worksheets("Sheet1").Cells(1, 1).Select

End Sub

sp_AnswersTopicSeeAnswer See Answer
Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 414
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
2
August 1, 2021 - 7:00 am
sp_Permalink sp_Print

Can you add the macro in a work book and some test data so we dont have to recreate?

Avatar
adrian@utas
University of Tasmania
Member
Members
Level 0
Forum Posts: 16
Member Since:
July 13, 2016
sp_UserOfflineSmall Offline
3
August 4, 2021 - 10:17 am
sp_Permalink sp_Print

Oh, so sorry I thought I had attached the workbook!
Here 'tis...

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 689
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
August 4, 2021 - 7:24 pm
sp_Permalink sp_Print

You could use something like this:

Sub ReplaceHyphens()
'
' ReplaceHyphens Macro
' Used to replace week ranges with specific week numbers
'

'
Dim strCol As String
strCol = InputBox("Please specify the column to be adjusted.... then go and make a coffee (it can take up to 8 min to process!)")
If strCol = "" Then
MsgBox "You didn't specify a column!", vbCritical
Exit Sub
End If

Dim DataSheet As Worksheet
Set DataSheet = Worksheets("Sheet1")

With DataSheet
maxrow = .Cells(.Rows.Count, strCol).End(xlUp).Row
Dim rng As range
Set rng = .range(.Cells(2, strCol), .Cells(maxrow, strCol))
End With

Dim dataSet
dataSet = rng.Value

For r = LBound(dataSet) To UBound(dataSet)
dataSet(r, 1) = SplitOutHyphens(dataSet(r, 1))
Next r

rng.Value = dataSet
End Sub
Function SplitOutHyphens(InputText) As String
Dim parts
parts = Split(InputText, ",")
Dim x As Long
For x = LBound(parts) To UBound(parts)
If InStr(parts(x), "-") <> 0 Then
Dim NumberRange
NumberRange = Split(parts(x), "-")
Dim lower As Long
lower = NumberRange(0)
Dim upper As Long
upper = NumberRange(1)
Dim y As Long
Dim OutputText As String
OutputText = vbNullString
For y = lower To upper
OutputText = OutputText & "," & y
Next y
parts(x) = Mid$(OutputText, 2)
End If
Next x
SplitOutHyphens = Join$(parts, ",")
End Function

sp_AnswersTopicAnswer
Answers Post
Avatar
adrian@utas
University of Tasmania
Member
Members
Level 0
Forum Posts: 16
Member Since:
July 13, 2016
sp_UserOfflineSmall Offline
5
August 8, 2021 - 1:56 pm
sp_Permalink sp_Print

Velouria, thank you that's perfect!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Peter Venkatrao, QSolutions Group
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
Hans Hallebeek: 185
Newest Members:
Kate Dyka
Kwaje Alfred Mogga
thong nguyen
Appiagyei Kofi Frimpong
Hilary Burchfield
Richie Wright
Adel Kock
Barbara Murray
Doug Milne
Siobhan Stringer
Forum Stats:
Groups: 3
Forums: 24
Topics: 6546
Posts: 28655

 

Member Stats:
Guest Posters: 49
Members: 32832
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.